Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andreyvital/react-native-android-sms-listener
Allows you to listen for incoming SMS messages using React Native
https://github.com/andreyvital/react-native-android-sms-listener
android react react-native sms sms-listener
Last synced: 10 days ago
JSON representation
Allows you to listen for incoming SMS messages using React Native
- Host: GitHub
- URL: https://github.com/andreyvital/react-native-android-sms-listener
- Owner: andreyvital
- License: mit
- Created: 2016-02-09T15:29:10.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-09-09T12:38:06.000Z (about 4 years ago)
- Last Synced: 2024-04-22T01:55:16.871Z (7 months ago)
- Topics: android, react, react-native, sms, sms-listener
- Language: Java
- Homepage:
- Size: 43 KB
- Stars: 348
- Watchers: 5
- Forks: 101
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-react-native - react-native-android-sms-listener ★194 - Allows you to listen for incoming SMS messages (Components / System)
README
## `react-native-android-sms-listener` [![react-native-android-sms-listener](https://badge.fury.io/js/react-native-android-sms-listener.svg)](https://badge.fury.io/js/react-native-android-sms-listener)
A utility that allows you to listen for incoming SMS messages.
### Example
```JS
import SmsListener from 'react-native-android-sms-listener'SmsListener.addListener(message => {
console.info(message)
})
```The contents of `message` object will be:
```JS
{
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:
```JS
let subscription = SmsListener.addListener(...)subscription.remove()
```In recent versions of Android you might also have to ask for permissions:
```JS
async function requestReadSmsPermission() {
try {
await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_SMS,
{
title: "(...)",
message: "Why you're asking for..."
}
);
} catch (err) {}
}class MyComponent extends Component {
// ...componentDidMount() {
requestReadSmsPermission();
}// ...
}
```### 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:
```JS
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(
message.originatingAddress,
verificationCode
).then(verifiedSuccessfully => {
if (verifiedSuccessfully) {
subscription.remove()
return
}if (__DEV__) {
console.info(
'Failed to verify phone `%s` using code `%s`',
message.originatingAddress,
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
```SH
$ npm install --save react-native-android-sms-listener
$ react-native link react-native-android-sms-listener
```### Manual Installation
For a manual installation, all you need to do to use this so-called utility is:
_android/settings.gradle_
```Gradle
include ':react-native-android-sms-listener'
project(':react-native-android-sms-listener').projectDir = new File(rootProject.projectDir,'../node_modules/react-native-android-sms-listener/android')
```_android/app/build.gradle_
```Gradle
dependencies {
compile project(':react-native-android-sms-listener')
// (...)
}
```_MainApplication.java_
```Java
import com.centaurwarchief.smslistener.SmsListenerPackage;
``````Java
@Override
protected List getPackages() {
return Arrays.asList(
new MainReactPackage(),
new SmsListenerPackage()
// (...)
);
}
```