Use this RN component to send an SMS with a callback (completed/cancelled/error). iOS and Android are both supported.
Currently, only user-initiated sending of an SMS is supported.
This means you can't use react-native-sms
to send an SMS in the background -- this package displays the native SMS view (populated with any recipients/body you want), and gives a callback describing the status of the SMS (completed/cancelled/error). PRs are welcome!
A new prop was added to enable direct sending of SMS from Android. see documentation below.
npm install react-native-sms --save
The compiler needs to know how to find your sweet new module!
react-native link react-native-sms
Note: If using RN < v0.47, use react-native-sms <= v1.4.2
Just a few quick & easy things you need to set up in order to get SendSMS up and running!
- Navigate to your MainActivity.java (
MyApp/android/app/src/main/java/some/other/directories/MainActivity.java
)
At the top of the file
import android.content.Intent; // <-- include if not already there
import com.tkporter.sendsms.SendSMSPackage;
Inside MainActivity
(place entire function if it's not there already)
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//probably some other stuff here
SendSMSPackage.getInstance().onActivityResult(requestCode, resultCode, data);
}
Then head to your [MyApp]Application.java
(MyApp/android/app/src/main/java/so/many/dirs/MyAppApplication.java
)
Make sure import com.tkporter.sendsms.SendSMSPackage;
is there
Then head down to getPackages()
, it has to look similar to this
protected List<ReactPackage> getPackages() {
//some variables
return Arrays.<ReactPackage>asList(
//probably some items like `new BlahPackage(),`
//just add into the list (don't forget commas!):
SendSMSPackage.getInstance()
);
}
FYI: this permission will automatically be merged into your built AndroidManifest.xml
(at MyApp/android/app/src/main/AndroidManifest.xml
)
<uses-permission android:name="android.permission.READ_SMS" />
If direct_send
is false
or undefined - then ensure your launchMode for MainActivity
is
android:launchMode="singleTask"
in order for the "back" button to return to your app after the message window is closed.
Once everything is all setup, it's pretty simple:
SendSMS.send(myOptionsObject, callback);
body
(String, optional)
The text that shows by default when the SMS is initiated
recipients
(Array (strings), optional)
Provides the phone number recipients to show by default
successTypes
(Array (strings), Andriod only, required)
direct_send
(boolean, optional)
If true, the Android app will send the SMS directly (without a native messaging app). It will loop on the recepients and send one by one.
An array of types that would trigger a "completed" response when using android
Possible values:
'all' | 'inbox' | 'sent' | 'draft' | 'outbox' | 'failed' | 'queued'
import SendSMS from 'react-native-sms'
//some stuff
someFunction() {
SendSMS.send({
body: 'The default body of the SMS!',
recipients: ['0123456789', '9876543210'],
successTypes: ['sent', 'queued']
}, (completed, cancelled, error) => {
console.log('SMS Callback: completed: ' + completed + ' cancelled: ' + cancelled + 'error: ' + error);
});
}
Having errors with import statements on Android? Something happened with linking
Go to your settings.gradle
(in MyApp/android/settings.gradle
) and add:
include ':react-native-sms'
project(':react-native-sms').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sms/android')
Then go to MyApp/android/app/build.gradle
and add inside dependencies { }
:
compile project(':react-native-sms')