Introduction
Here is an Example to Send Text SMS on Button Click in React Native. Instead of remembering or copying the number to send the Text SMS you can provide one tab send SMS experience to the users of your application.
We will use react-native-sms
package for this example.
You can also visit Make Phone Call, Send SMS or Email Using React Native Communication using react-native-communications
to Make Phone Calls, Send Text SMS, Send Email and Open a URL in Browser.
You can also see:
- React Native Share API to Share TextInput message
- Share Facebook Post with URL from React Native App
- Send WhatsApp Message from React Native App
- Tweet on Twitter with URL from React Native App
To Send Direct Message without any user intervention you can visit React Native Bridge Example to Send Direct SMS from React Native App
To Send SMS
SendSMS.send(
{
// Message body
body: bodySMS,
// Recipients Number
recipients: [mobileNumber],
// An array of types
// "completed" response when using android
successTypes: ['sent', 'queued'],
},
(completed, cancelled, error) => {
if (completed) {
console.log('SMS Sent Completed');
} else if (cancelled) {
console.log('SMS Sent Cancelled');
} else if (error) {
console.log('Some error occured');
}
},
);
In this example of sending a Text SMS from React Native App, we will send the SMS by clicking on a button using react-native-sms
library. So let’s get started.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react native command line interface to make our React Native App.
If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:
npm uninstall -g react-native-cli @react-native-community/cli
Run the following commands to create a new React Native project
npx react-native init ProjectName
If you want to start a new project with a specific React Native version, you can use the --version argument:
npx react-native init ProjectName --version X.XX.X
Note If the above command is failing, you may have old version of react-native
or react-native-cli
installed globally on your pc. Try uninstalling the cli and run the cli using npx.
This will make a project structure with an index file named App.js in your project directory.
Installation of Dependency
To use SendSMS
we need to install react-native-sms
package. To install this
Open the terminal and jump into your project
cd ProjectName
Run the following command
npm install react-native-sms --save
This command will copy all the dependencies into your node_module directory.
CocoaPods Installation
Please use the following command to install CocoaPods
npx pod-install
Permission to Send SMS for Android
We are trying to use the SMS feature so we need to add some permission in AndroidManifest.xml
file for Android. For more about the permission, you can see this post.
So we are going to add the following permissions in the AndroidMnifest.xml
<uses-permission android:name="android.permission.READ_SMS"/>
Here we have done all the configurations we need.
Code to Send Text SMS
Now open App.js in any code editor and replace the code with the following code
App.js
// Example to Send Text SMS on Button Click in React Native
// https://aboutreact.com/send-text-sms-in-react-native/
// import React in our code
import React, {useState} from 'react';
// import all the components we are going to use
import {
SafeAreaView,
StyleSheet,
View,
Text,
TouchableOpacity,
TextInput,
} from 'react-native';
// import SMS API
import SendSMS from 'react-native-sms';
const App = () => {
const [mobileNumber, setMobileNumber] = useState('9999999999');
const [bodySMS, setBodySMS] = useState(
'Please follow https://aboutreact.com',
);
const initiateSMS = () => {
// Check for perfect 10 digit length
if (mobileNumber.length != 10) {
alert('Please insert correct contact number');
return;
}
SendSMS.send(
{
// Message body
body: bodySMS,
// Recipients Number
recipients: [mobileNumber],
// An array of types
// "completed" response when using android
successTypes: ['sent', 'queued'],
},
(completed, cancelled, error) => {
if (completed) {
console.log('SMS Sent Completed');
} else if (cancelled) {
console.log('SMS Sent Cancelled');
} else if (error) {
console.log('Some error occured');
}
},
);
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.container}>
<Text style={styles.titleText}>
Example to Send Text SMS on Button Click in React Native
</Text>
<Text style={styles.titleTextsmall}>
Enter Mobile Number
</Text>
<TextInput
value={mobileNumber}
onChangeText={
(mobileNumber) => setMobileNumber(mobileNumber)
}
placeholder={'Enter Conatct Number to Call'}
keyboardType="numeric"
style={styles.textInput}
/>
<Text style={styles.titleTextsmall}>
Enter SMS body
</Text>
<TextInput
value={bodySMS}
onChangeText={(bodySMS) => setBodySMS(bodySMS)}
placeholder={'Enter SMS body'}
style={styles.textInput}
/>
<TouchableOpacity
activeOpacity={0.7}
style={styles.buttonStyle}
onPress={initiateSMS}>
<Text style={styles.buttonTextStyle}>
Send Message
</Text>
</TouchableOpacity>
</View>
</SafeAreaView>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: 'white',
padding: 10,
textAlign: 'center',
},
titleText: {
fontSize: 22,
textAlign: 'center',
fontWeight: 'bold',
},
titleTextsmall: {
marginVertical: 8,
fontSize: 16,
},
buttonStyle: {
justifyContent: 'center',
marginTop: 15,
padding: 10,
backgroundColor: '#8ad24e',
},
buttonTextStyle: {
color: '#fff',
textAlign: 'center',
},
textInput: {
height: 40,
borderColor: 'gray',
borderWidth: 1,
width: '100%',
paddingHorizontal: 10,
},
});
To Run the React Native App
Open the terminal again and jump into your project using.
cd ProjectName
1. Start Metro Bundler
First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:
npx react-native start
Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.
2. Start React Native Application
To run the project on an Android Virtual Device or on real debugging device:
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios
Output Screenshots
This is how you can send SMS in React Native. If you have any doubts or want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂
Hi thank you so much for the detailed tutorial. I am trying to use this to send a message to multiple recipients and I am trying to pass multiple recipients as an array ( recipients: [‘0123456789’, ‘9876543210’],) but for some reason on Android it opens the message compose screen with just the first number. Do you by any chance know how I can include multiple recipients?
Thank You for your appreciation it will motivate me to do more.
I have tried it and it works for my device. I think its device-dependent as you can see in the following code snippet from the library it is just taking the recipients and adding a separator semicolon(;) and comma (;) for the Samsung device only. So it depends on the device which type of separator device want. Like if the library added comma and device needs semicolon as separator then the device took all the contacts as a string and will take all numbers as a single number.
You can find more details about it on this thread.
Library code snipet:
I'll try to find a permanent solution for all the devices. Meanwhile, if you find any solution then please share for others too. 🙂
Did you find a solution?
Nope. This library do not support the same.
Hi!
This tutorial is great! Thanks!
I was wondering though if you have a version for API levels higher than 23? (I’m targeting higher API devices, but I don’t want to ask for permission everytime the send button is clicked.)
Thank you for your time!
Moore
It will ask for the one time only and will check next time if the permission is provided then it will ignore and will proceed and if user disabled the permission for the app in between then will ask for the permission again.
Hi,
This tutorial is great. It helped me a lot.
I have one question, please help. Is it possible to close the message screen automatically once text is sent? I am new to React-native, so not sure if my question is relevant here.
Thanks
Nice to hear that it helped you..
In react native we are just triggering the message app with some data not controlling it.. So it is not posible in current case. May be you have to integrate some native code for that.
I copied all code on Expo Snack but It’s not working there . Only First Screen(Send SMS Icon) is render.
It will not work with Expo
Sir how msg will be sent without moving to mobile sms messenger,i want msg should directly sent from react native app,without opening mobile sms messange
No, you can’t send messages directly. You have to use a server-side message gateway if you want auto-send.
Can you make any tutorial for that variant of “problem”….I want to make app that automaticly send message to predefined number. With EXPO I did use expo-sms it works as code above, but have no idea how to do server side to send sms msg
The permission which used here is sensitive user information of the user. Also if I send for review most probably it get rejected. Any solution for this? Or what is your concern here?
If you are integrating send SMS or call related functionality then you have to have proper explanation else your app will probably get rejected.
does it work for both android and ios??
Yeah I will work for both.
not working on ios simulator
Yeah it will not send message from iOS simulator, try to run it on real device.
Thanks sir, this helped a lot. Most helpful website for react native devs, I would say. I just have one query, what to do if we have to message multiple contacts, which are selected in a contacts app. Please do help. It is very important for me. I am loading the contacts using react-native-contacts, you suggest me another. Please help me. Thanks
Hi Astha,
Have you tried passing array in recipients?
Here -> recipients: [mobileNumber],
its not working on expo Go in mobile i follow the same instruction
Sorry not sure about Expo, I suggest eject the project and then try.
How can i use this code along with the code ” get current location using Geolocation” in order to send the current location as SMS?