3

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 ?

Friis1978
  • 1,179
  • 9
  • 16

3 Answers3

3

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));
    });
Friis1978
  • 1,179
  • 9
  • 16
  • Thanks for sharing. Did you find a way to do this for iOS? – Greg Jul 28 '18 at 14:20
  • Hello ScreenWatcher, the function is made as a firebase function, which will work with both android & ios apps, if they are setup with firebase. I use react native. – Friis1978 Jul 28 '18 at 18:08
2

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.

kdenz
  • 671
  • 1
  • 7
  • 16
  • Hello kdenz. Thank you very much for the answer, I have already seen both links. I dont think I understand how to make it work, are you a saying, what it could be done by sending a http request to the Twilio api though axios perhaps ? Or could I perhaps setup a Lambda function in the AWS ? It seems very complicated sending a sms message like that ? – Friis1978 Apr 07 '18 at 09:51
  • 1
    Perhaps, I should give up, and just let the user push the sms button. But that does really ruin the purpose of the app, because the job of the app is to notify another phone if something is happening in the users app without forcing the user to do anything. – Friis1978 Apr 07 '18 at 09:55
  • What about using firebase, and send the sms, when a value is changed in here ? – Friis1978 Apr 07 '18 at 09:56
  • @Friis1978 yes that’ll work too. You can set up a firebase cloud function which listens to certain database values, and when it changes it automatically sends sms through twilio. https://firebase.google.com/docs/functions/ – kdenz Apr 07 '18 at 10:28
  • Hei again, It worked finally, just followed this tutorial setting up a firebase function: https://www.youtube.com/watch?v=vIWXoaR_VnQ&t=420s – Friis1978 Apr 07 '18 at 14:24
0

On android, it is possible to send sms from user's number programmatically without user interaction.

But on IOS, the only way you can send an SMS without user interaction is to use an external provider such as Twilio; but the message will come from your server's number, not from the user.

I have already answered for same kind of question. Check this out Is there anyway to send sms in background for both Android & IOS?

Md. Robi Ullah
  • 1,703
  • 3
  • 20
  • 32