如何以编程方式从 React Native App 发送短信?

How to send sms messages from a React Native App programmatically ?

我希望能够通过我的 React Native 应用程序在后台以编程方式发送短信。

我知道如何在代码中正常发送短信,但该应用程序一直打开默认短信应用程序,这不是我想要的。

用户不应按任何按钮来发送短信,因为我的目标是每次用户在应用程序中执行特定任务时通知电话号码。

我试过查看 Twilio,但他们没有为 React Native 提供 api。

有人知道我该怎么做吗?

我认为只有 Android 才有可能,如这个已弃用的包 https://www.npmjs.com/package/react-native-send-sms

所示

对于iOS,我不这么认为,如

要实现您想要的效果,您需要像 Twilio 这样的 SMS 服务。然后,您可以设置一个服务器(或云功能,以实现最低成本 + 易于维护),从您的 RN 应用程序接收 API 调用,并将消息发送给所需的收件人。然后您也可以设置一些安全措施,以防止黑客向 API.

发送垃圾邮件

或者如果您不关心安全性(我强烈不建议这样做),您可以直接在您的应用中调用 Twilio 发送消息 API,这很危险,因为黑客可以轻松提取您的 Twilio授权令牌并使用它为自己发送消息。

根据 kdenz 的回答,我按照这里的教程进行操作:Seeting up a firebase function

这是我在 firebase 数据库值 'visible' 发生变化时向 Twilio 发送请求的代码。

import * as functions from 'firebase-functions';
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

const twilio = require('twilio');
const accountSid = functions.config().twilio.sid;
const authToken = functions.config().twilio.token;

console.log(`Twilio account: ${accountSid}`);

const client = new twilio(accountSid, authToken);

const twilioNumber = 'xxx-xxx-xxx';

exports.textStatus = functions.database
    .ref('/users/{userId}/data/visible')
    .onUpdate(event => {
        return admin.database()
            .ref(`users/{userId}/data/`)
            .once('value')
            .then(snapshot => snapshot.val())
            .then(user=> {
                console.log(user[0]);

                const longitude = user.longi;
                const latitude = user.lati;
                const phoneNumber = user.phone;

                const textMessage = {
                    body: `See user position at Google: http://www.google.com/maps/place/${latitude},${longitude}`,
                    to: phoneNumber,
                    from: twilioNumber
                }

                return client.messages.create(textMessage);


            })
            .then(message => console.log(message.sid, 'success'))
            .catch(err => console.log(err));
    });

在 android 上,可以通过编程方式从用户号码发送短信 without user interaction

但是在 IOS,您可以在没有用户交互的情况下发送 SMS 的唯一方法是使用外部提供商,例如 Twilio;但消息将来自您的服务器号码,而不是来自用户。

同类问题我已经回答过了。看一下这个 Is there anyway to send sms in background for both Android & IOS?