Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to be able to sent sms messages though my React Native app programatically in the background.

I know how to sent sms normally in the code, but the app keeps opening the default sms app, and that is not what i want.

The user should not push any buttons to sent the sms, because my goal is to notify a phonenumber every time the user is doing a particularly task in the app.

I have tried looking at Twilio, but they dont provide a api for React Native.

Does anybody know something about how I can do this ?

like image 452
Friis1978 Avatar asked May 29 '26 06:05

Friis1978


2 Answers

With the answer from kdenz, I followed a tutorial here: Seeting up a firebase function

This is my code for sending a request to Twilio, when the firebase database value 'visible' is changing.

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));
    });
like image 113
Friis1978 Avatar answered Jun 01 '26 07:06

Friis1978


I think this is only possible with Android, as seen in this deprecated package https://www.npmjs.com/package/react-native-send-sms

For iOS, I don't think so, as seen in How to programmatically send a text message after the user has given permission?

To achieve what you want, you'll need a SMS service like Twilio. Then you can set up a server (Or a cloud function for minimal cost + easy maintainability) which receives API calls from your RN app, and sends the message to the desired recipient. You can then set up some security measures too, to prevent hackers from spamming the API.

Or if you don't care about security (Which I highly don't recommend), you can directly call Twilio send message API within your app, which is dangerous because hackers can easily extract your Twilio authorization token and use it to send messages for themselves.

like image 22
kdenz Avatar answered Jun 01 '26 07:06

kdenz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!