Example to Send Text SMS on Button Click in React Native

Example to Send Text SMS on Button Click in React Native

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 Call, Send Text SMS, Send Email and Open a URL in Browser.

To Send SMS

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 init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
npm install -g react-native-cli
Run the following commands to create a new React Native project
react-native init ProjectName
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

Run the following command

This command will copy all the dependencies into your node_module directory, You can find the directory in node_modulethe directory named react-native-sms.

Linking of Library

To use react-native-sms library we have to link some dependencies in Android and IOS project files. In order to link the library you have to run following command:

You can see the following changes in your android project. If you are unable to find the changes then please do those changes as they are must for this example.

Go to the SMSExample> android > app > build.gradle.
Scroll down and you can see the following dependency and if not please add this.

Open SMSExample> android > settings.gradle and you can see the following lines and if not please add this.

 

Open SMSExample> android > app > src > main > java > com > smsexample> MainApplication.java  and you can see the following lines in imports if not then please add

Scroll down and you can see the following line in getPackages after new MainReactPackage() if not then please add a comma(,) and add

 

Permission to Send SMS for Android

We are trying to use SMS  feature so we need to add some permission in AndroidManifest.xml file.  On devices before SDK version 23, the permissions are automatically granted if they appear in the manifest but after SDK version 23 android applies new permissions model. For more about the permission, you can see this post.

So we are going to add the following permissions in the AndroidMnifest.xml

By adding permission in AndroidManifest.xml you are able to use SMS in devices which are lower than API 23. But if you want to access the SMS in higher level devices you also need to ask for the runtime permission. In this example, we are targeting the devices with lower than 23 API level we are not planning for the runtime permission. If you still want to do it for you then please comment below we will do it for you.

Here we have done all the configurations we need. Now open App.js in any code editor and replace the code with the following code

App.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Here is the Screenshot of the application.

This is how you can send SMS in React Native. If you have any doubt or you 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. 🙂

How useful was this post?

Click on a star to rate us!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

5 thoughts on “Example to Send Text SMS on Button Click in React Native”

  1. 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?

  2. 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

  3. 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.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

About React will use the information you provide on this form to be in touch with you and to provide updates and marketing.