@ernestbies/react-native-android-sms-listener

1.0.2 • Public • Published

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

Package Sidebar

Install

npm i @ernestbies/react-native-android-sms-listener

Weekly Downloads

17

Version

1.0.2

License

MIT

Unpacked Size

11 kB

Total Files

12

Last publish

Collaborators

  • ernestbies