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

标签 reactjs react-native twilio twilio-api

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

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

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

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

有人知道我该怎么做吗?

最佳答案

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

这是我在 firebase 数据库值“可见”发生变化时向 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));
    });

关于reactjs - 如何以编程方式从 React Native App 发送短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49700102/

相关文章:

node.js - Node js twilio 发送短信不起作用

javascript - ReactJS + 终极版 : How to structure action creators down to each component?

javascript - 我必须单击按钮两次才能在 React 中正确渲染状态

javascript - 条件渲染 react native :only else part is working

ios - Xcode9 iOS 导出存档以正确的格式失败

php - 未接听电话的通话收费 - Twilio

django - 使用 Google App Engine 和 Django 将第三方库 (twilio) 添加到项目

javascript - 设置嵌套数组的状态

javascript - 函数参数属性的赋值 'a' Eslint

react-native - Firebase - Gradle 构建失败 - com.google.android.gms :play-services-measurement-base