All Projects → briankabiro → React Native Get Sms Android

briankabiro / React Native Get Sms Android

Licence: mit
React Native module to get messages on an Android device

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to React Native Get Sms Android

Grunt Myth
Myth - Postprocessor that polyfills CSS
Stars: ✭ 70 (-21.35%)
Mutual labels:  npm-package
Incompose
A inferno utility belt for function components and higher-order components
Stars: ✭ 76 (-14.61%)
Mutual labels:  npm-package
Webhook Discord
A simple Javascript file for nicely formatting Discord webhooks
Stars: ✭ 81 (-8.99%)
Mutual labels:  npm-package
Github Files Fetcher
Download a specific folder or file from a GitHub repo through command line
Stars: ✭ 73 (-17.98%)
Mutual labels:  npm-package
Deps Report
Generate reports about dependencies and dependents of your JavaScript/TypeScript files through an AST. It supports import and require statements.
Stars: ✭ 76 (-14.61%)
Mutual labels:  npm-package
Ptt Client
A Node.js/Browser client for fetching data from ptt.cc
Stars: ✭ 78 (-12.36%)
Mutual labels:  npm-package
Awesome Npm Packages
🚀 A collection of awesome npm packages for Noders.
Stars: ✭ 69 (-22.47%)
Mutual labels:  npm-package
Node Fast Ratelimit
☔️ Fast and efficient in-memory rate-limit for Node, used to alleviate most common DOS attacks.
Stars: ✭ 84 (-5.62%)
Mutual labels:  npm-package
Vue C3
vue-c3 is a reusable vue component for c3 charts
Stars: ✭ 76 (-14.61%)
Mutual labels:  npm-package
Homebridge Pihole
Pihole switch for Homebridge
Stars: ✭ 80 (-10.11%)
Mutual labels:  npm-package
Delay Cli
Delay execution for a given amount of seconds
Stars: ✭ 74 (-16.85%)
Mutual labels:  npm-package
Vue Scroll Progress Bar
Vue.js plugin for page scroll progress bar
Stars: ✭ 76 (-14.61%)
Mutual labels:  npm-package
Webcam Easy
javascript access webcam stream and take photo
Stars: ✭ 79 (-11.24%)
Mutual labels:  npm-package
Random Bytes Readable Stream
Creates a readable stream producing cryptographically strong pseudo-random data using `crypto.randomBytes()`
Stars: ✭ 71 (-20.22%)
Mutual labels:  npm-package
React Infinite Scroll Component
An awesome Infinite Scroll component in react.
Stars: ✭ 1,235 (+1287.64%)
Mutual labels:  npm-package
Term Img Cli
Display images in iTerm
Stars: ✭ 70 (-21.35%)
Mutual labels:  npm-package
Minimal Feedback
🗳 minimal-feedback is a blazingly fast and highly customizable component to get user feedback.
Stars: ✭ 78 (-12.36%)
Mutual labels:  npm-package
Forge Node App
🛠📦🎉 Generate Node.js boilerplate with optional libraries & tools
Stars: ✭ 90 (+1.12%)
Mutual labels:  npm-package
Add Asset Webpack Plugin
Dynamically add an asset to the Webpack graph
Stars: ✭ 84 (-5.62%)
Mutual labels:  npm-package
Node Loadbalance
A collection of distilled load balancing engines
Stars: ✭ 79 (-11.24%)
Mutual labels:  npm-package

react-native-get-sms-android

Module that supports interaction with the Messaging API on Android

The package allows you to:

  • get messages
  • send messages
  • delete messages

Decided to start this package because react-native-android-sms wasn't maintained at the time.


Getting started

Yarn

$ yarn add react-native-get-sms-android

Npm

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

Mostly automatic installation

$ react-native link react-native-get-sms-android

Manual installation

android/settings.gradle

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

android/app/build.gradle

dependencies{
    compile project(':react-native-get-sms-android')
 }

MainApplication.java

import com.react.SmsPackage;

@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
    new MainReactPackage(),
    new SmsPackage()
    // (...)
  );
}

Android Permissions

Note: This has changed from 2.x. See Upgrading to 2.x section if using <=2.x

Add permissions to your android/app/src/main/AndroidManifest.xml file.

...
  <uses-permission android:name="android.permission.READ_SMS" />
  <uses-permission android:name="android.permission.WRITE_SMS" />
  <uses-permission android:name="android.permission.SEND_SMS" />
...

Upgrading to 2.x

You need to add permissions manually. react-native-get-sms-android does not automatically require permissions from 2.x. Refer to this issue.

You need to require permissions in your AndroidManifest.xml file's application element based on what functions you plan to use like the official documentation describes:

Function Permission needed
SmsAndroid.list android.permission.READ_SMS
SmsAndroid.delete android.permission.WRITE_SMS
SmsAndroid.autoSend android.permission.SEND_SMS

Usage

List SMS Messages

import SmsAndroid from 'react-native-get-sms-android';

/* List SMS messages matching the filter */
var filter = {
  box: 'inbox', // 'inbox' (default), 'sent', 'draft', 'outbox', 'failed', 'queued', and '' for all

  /**
   *  the next 3 filters can work together, they are AND-ed
   *  
   *  minDate, maxDate filters work like this:
   *    - If and only if you set a maxDate, it's like executing this SQL query:
   *    "SELECT * from messages WHERE (other filters) AND date <= maxDate"
   *    - Same for minDate but with "date >= minDate"
   */
  minDate: 1554636310165, // timestamp (in milliseconds since UNIX epoch)
  maxDate: 1556277910456, // timestamp (in milliseconds since UNIX epoch)
  bodyRegex: '(.*)How are you(.*)', // content regex to match

  /** the next 5 filters should NOT be used together, they are OR-ed so pick one **/
  read: 0, // 0 for unread SMS, 1 for SMS already read
  _id: 1234, // specify the msg id
  thread_id: 12, // specify the conversation thread_id
  address: '+1888------', // sender's phone number
  body: 'How are you', // content to match
  /** the next 2 filters can be used for pagination **/
  indexFrom: 0, // start from index 0
  maxCount: 10, // count of SMS to return each time
};

SmsAndroid.list(
  JSON.stringify(filter),
  (fail) => {
    console.log('Failed with this error: ' + fail);
  },
  (count, smsList) => {
    console.log('Count: ', count);
    console.log('List: ', smsList);
    var arr = JSON.parse(smsList);

    arr.forEach(function(object) {
      console.log('Object: ' + object);
      console.log('-->' + object.date);
      console.log('-->' + object.body);
    });
  },
);

/*
Each sms will be represents by a JSON object represented below

{
  "_id": 1234,
  "thread_id": 3,
  "address": "2900",
  "person": -1,
  "date": 1365053816196,
  "date_sent": 0,
  "protocol": 0,
  "read": 1,
  "status": -1,
  "type": 1,
  "body": "Hello There, I am an SMS",
  "service_center": "+60162999922",
  "locked": 0,
  "error_code": -1,
  "sub_id": -1,
  "seen": 1,
  "deletable": 0,
  "sim_slot": 0,
  "hidden": 0,
  "app_id": 0,
  "msg_id": 0,
  "reserved": 0,
  "pri": 0,
  "teleservice_id": 0,
  "svc_cmd": 0,
  "roam_pending": 0,
  "spam_report": 0,
  "secret_mode": 0,
  "safe_message": 0,
  "favorite": 0
}

*/

Delete SMS Message

Delete an sms with id. If the message with the specified id does not exist it will fail with error: SMS not found

import SmsAndroid from 'react-native-get-sms-android';

SmsAndroid.delete(
  _id,
  (fail) => {
    console.log('Failed with this error: ' + fail);
  },
  (success) => {
    console.log('SMS deleted successfully');
  },
);

Important note on deleting messages

For Android > 5, the only app permitted to delete an SMS message is the app installed as the default SMS handler.

If your app is not set as the default SMS handler, it will not be able to delete. See this thread on Stack Overflow for more details.

Send SMS Message (automatically)

Send an sms directly with React without user interaction.

import SmsAndroid from 'react-native-get-sms-android';

SmsAndroid.autoSend(
  phoneNumber,
  message,
  (fail) => {
    console.log('Failed with this error: ' + fail);
  },
  (success) => {
    console.log('SMS sent successfully');
  },
);

Send message to multiple numbers

import SmsAndroid from 'react-native-get-sms-android';

let phoneNumbers = {
  "addressList": ["123", "456"]
};

SmsAndroid.autoSend(
  JSON.stringify(phoneNumbers),
  message,
  (fail) => {
    console.log('Failed with this error: ' + fail);
  },
  (success) => {
    console.log('SMS sent successfully');
  },
);

Event listeners

An event will be thrown when the sms has been delivered. If the sms was delivered successfully the message will be "SMS delivered" otherwise the message will be "SMS not delivered"

import { DeviceEventEmitter } from 'react-native';

DeviceEventEmitter.addListener('sms_onDelivery', (msg) => {
  console.log(msg);
});

Note

  • Does not work with Expo as it's not possible to include custom native modules beyond the React Native APIs and components that are available in the Expo client app. The information here might help with integrating the module while still using Expo.

Contributions welcome!

Feel free to open an issue or a Pull Request.

Thanks

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].