Git Product home page Git Product logo

react-native-get-sms-android's Introduction

react-native-get-sms-android

Module that gets all the messages from android

Decided to start this package because react-native-android-sms isn't maintained. It doesn't work with newer React native versions


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

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" />
...

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 4 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
    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: " + obj);
            console.log("-->" + obj.date);
            console.log("-->" + obj.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");
});

Send SMS Message (automatically)

Send an sms directy 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");
});

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); 
    });

thanks

  • react-native-android-sms

react-native-get-sms-android's People

Contributors

briankabiro avatar davidstoneham avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.