@ernestbies/react-native-android-sms-listener
v1.0.2
Published
Allows you to listen for incoming SMS messages.
Downloads
71
Maintainers
Readme
react-native-android-sms-listener
A utility that allows you to listen for incoming SMS messages (working in background).
Example
import SmsListener from '@ernestbies/react-native-android-sms-listener'
SmsListener.addListener(message => {
console.info(message)
})
The contents of message
object will be:
{
originatingAddress: string,
body: string,
timestamp: number
}
SmsListener#addListener
returns a CancellableSubscription
so if you want to stop listening for incoming SMS messages you can simply .remove
it:
let subscription = SmsListener.addListener(...)
subscription.remove()
In recent versions of Android you might also have to ask for permissions READ_SMS and RECEIVE_SMS:
async function requestReadSmsPermission() {
try {
await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_SMS,
);
await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.RECEIVE_SMS,
);
} catch (err) {}
}
Installation
$ npm install --save @ernestbies/react-native-android-sms-listener