Skip to content

gitter-badger/react-native-sms-retriever

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Platform License

Cover

With the SMS Retriever API, you can perform SMS-based user verification in your Android app automatically, without requiring the user to manually type verification codes, and without requiring any extra app permissions.


Read Phone Number Read SMS


Installation

npm install --save react-native-sms-retriever
react-native link react-native-sms-retriever

Manual (if you don't like to use react-native link)

  1. Add the following lines to android/settings.gradle:
include ':react-native-sms-retriever'
project(':react-native-sms-retriever').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sms-retriever/android')
  1. Add the compile line to the dependencies in android/app/build.gradle:
dependencies {
  // ...
  compile project(':react-native-sms-retriever')
}
  1. Add the import and link the package in MainApplication.java:
import me.furtado.smsretriever.RNSmsRetrieverPackage; // <-- Add the import

public class MainApplication extends Application implements ReactApplication {

  // ...

  @Override
  protected List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
      new MainReactPackage(),
      // ...
      new RNSmsRetrieverPackage() // <-- Add it to the packages list
    );
  }

  // ...
}

Basic Usage

import SmsRetriever from 'react-native-sms-retriever';

// Get the phone number (first gif)
 _onPhoneNumberPressed = async () => {
  try {
    const phoneNumber = await SmsRetriever.requestPhoneNumber();
  } catch (error) {
    console.log(JSON.stringify(error));
  }
 };

// Get the SMS message (second gif)
_onSmsListenerPressed = async () => {
  try {
    const registered = await SmsRetriever.startSmsRetriever();
    if (registered) {
      SmsRetriever.addSmsListener(event => {
        console.log(event.message);
        SmsRetriever.removeSmsListener();
      }); 
    }
  } catch (error) {
    console.log(JSON.stringify(error));
  }
};

About Phone

To get the phone number, use a real device. The SMS Retriever API doesn't work very well with emulators.

About SMS

The SMS Retriever API has some rules for message. A valid verification message might look like the following:

<#> Your Example app code is: 123ABC78
/SBnkJ8069Q

The <#> is required. The /SBnkJ8069Q is a hash string that identifies your app (in the case it's the hash of the example app). Too see how to generate the hash access this link. Alternatively, you can get your app's hash string with the AppSignatureHelper class.

Methods and Errors

requestPhoneNumber(): Promise<String>

Obtain the user's phone number (using the hint picket).

Error Type Error Message
ACTIVITY_NULL_ERROR_TYPE Activity is null.
ACTIVITY_RESULT_NOOK_ERROR_TYPE There was an error trying to get the phone number.
CONNECTION_SUSPENENDED_ERROR_TYPE Client is temporarily in a disconnected state.
CONNECTION_FAILED_ERROR_TYPE There was an error connecting the client to the service.
SEND_INTENT_ERROR_TYPE There was an error trying to send intent.
UNAVAILABLE_ERROR_TYPE Google Play Services is not available.
UNSUPORTED_VERSION_ERROR_TYPE The device version of Google Play Services is not supported.

startSmsRetriever(): Promise<Boolean>

Start to listen for SMS messages.

Error Type Error Message
TASK_FAILURE_ERROR_TYPE Task failed.
UNAVAILABLE_ERROR_TYPE Google Play Services is not available.
UNSUPORTED_VERSION_ERROR_TYPE The device version of Google Play Services is not supported.

addSmsListener(event: Function): Promise<Boolean>

Get the SMS content. Get the SMS content with: event.message.

Error Param Type Description
extras String Sent only when there was a problem getting the SMS content.
status String Sent only when status is not OK or TIMEOUT.
timeout String Sent only when status is equals to TIMEOUT.

removeSmsListener(): Void

Stop to listen for SMS messages.

Change-log

A brief summary of each React Native SMS Retriever release can be found on the releases.

License

This code is distributed under the terms and conditions of the MIT License.

About

Android SMS Retriever API for React Native.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published