Socket
Socket
Sign inDemoInstall

react-native-android-sms-listener

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-android-sms-listener

Allows you to listen for incoming SMS messages


Version published
Weekly downloads
109
increased by2.83%
Maintainers
1
Weekly downloads
 
Created
Source

react-native-android-sms-listener react-native-android-sms-listener

An utility that allows you to listen for incoming SMS messages.

Example

import SmsListener from 'react-native-android-sms-listener'

SmsListener.addListener(message => {
  console.info(message)
})

The contents of message object will be:

{
  originatingAddress: string,
  body: string
}

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()
Example of using it for verification purposes:

...and if in your sign up process you have the phone number verification step which is done by sending a code via SMS to the specified phone, you might want to verify it automatically when the user receive it — pretty much like what Telegram or WhatsApp does:

let subscription = SmsListener.addListener(message => {
  let verificationCodeRegex = /Your verification code: ([\d]{6})/

  if (verificationCodeRegex.test(message.body)) {
    let verificationCode = message.body.match(verificationCodeRegex)[1]

    YourPhoneVerificationApi.verifyPhoneNumber(
      ...,
      verificationCode
    ).then(verifiedSuccessfully => {
      if (verifiedSuccessfully) {
        subscription.remove()
        return
      }

      if (__DEV__) {
        console.info(
          'Failed to verify phone `%s` using code `%s`',
          ...,
          verificationCode
        )
      }
    })
  }
})

If you're using Twilio or a similar third-party messaging service which you have a fixed phone number to deliver messages you might want to ensure that the message comes from your service by checking message.originatingAddress.

Installation

$ npm install --save react-native-android-sms-listener

...and all you need to do to use this so-called utility is:

android/settings.gradle

include ':ReactNativeAndroidSmsListener'

project(':ReactNativeAndroidSmsListener').projectDir = new File(
  rootProject.projectDir,
  '../node_modules/react-native-android-sms-listener/android'
)

android/app/build.gradle

dependencies {
  compile project(':ReactNativeAndroidSmsListener')
  // (...)
}

MainActivity.java

import com.centaurwarchief.smslistener.SmsListener;
@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
    new MainReactPackage(),
    new SmsListener(this)
    // (...)
  );
}

Keywords

FAQs

Package last updated on 04 Jun 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc