andreyvital / react-native-android-sms-listener

Allows you to listen for incoming SMS messages using React Native
MIT License
358 stars 99 forks source link

listener not working for android #28

Closed rrahul14 closed 4 years ago

rrahul14 commented 6 years ago

react-native :0.47.2 android device OS: marshmallow, nougat, device manufacturer: Moto inside package.json "react-native-android-sms-listener": "^0.5.0", //code inside let subscription = SmsListener.addListener(message => { let verificationCodeRegex = /Your verification code: ([\d]{7})/ if (verificationCodeRegex.test(message.body)) { let verificationCode = message.body.match(verificationCodeRegex)[1] newThis.setState({ verificationCode: verificationCode }) } alert(message) console.log('message', message) })

I have followed all installation steps and i have used alert inside listener, even added log for message but listener not working

hkxicor commented 6 years ago

same problem

ohasy commented 6 years ago

It will not, until you get the required run time permissions in android. Luckily for us, Permissions can be taken through react-native "PermissionsAndroid" so we do't have to mess with android native code and don't have to get permissions there. Which I don't know how to do. since there are only two files. so here is what you can do.

import { PermissionsAndroid } from "react-native";
import SmsListener from "react-native-android-sms-listener";

async function requestReadSmsPermission() {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.READ_SMS,
      {
        title: "Auto Verification OTP",
        message: "need access to read sms, to verify OTP"
      }
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("sms read permissions granted", granted);
    } else {
      console.log("sms read permissions denied", denied);
    }
  } catch (err) {
    console.warn(err);
  }
}

export default class SomeCoolClassName extends Component {
  constructor(props) {
    super(props);
    this.SMSReadSubscription = {};
  }

  componentDidMount() {
    requestReadSmsPermission();
    console.log("sup dude");
    this.SMSReadSubscription = SmsListener.addListener(message => {
      console.log("Message:", message);
      //message.body will have the message.
      //message.originatingAddress will be the address.
    });
  }

  componentWillUnmount() {
    //remove listener
    this.SMSReadSubscription.remove();
  }

and add this into Project\android\app\src\main\AndroidManifest.xml file.

    <!-- To use SMS based services -->
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
18241893045 commented 6 years ago

same problem! i need help!!!!

rajivPHP commented 6 years ago

same issue listener not working

ohasy commented 6 years ago

have you tried everything I have mentioned in my comment.

rajivPHP commented 6 years ago

yes tried all it is not listening.set the permissions in manifest.and got the sample from yours and tried.

ohasy commented 6 years ago

does your app asks for the Read SMS permission ?

rajivPHP commented 6 years ago

yes it asked and i gave the permission to access sms related

rajivPHP commented 6 years ago

tried with background task running also to wait and listen.but not listening too const myJobKey = 'TF'; BackgroundJob.register({ jobKey: myJobKey, job: () => { SmsListener.addListener(message => { this.setState({ lastMessage: message.body }); console.log(message.body); }); } }); BackgroundJob.schedule({ jobKey: myJobKey, period: 1000, timeout: 1000, alwaysRunning: true, persist: false });

appielife commented 6 years ago

Seems like after an update in play service or gradle has caused this issue. Can't run it now, earlier was working Any help will be appreciated

arunjkumarp commented 6 years ago

It worked for me - @yashojha19 It saved a day for me

HemantRMali commented 5 years ago

@yasoza , Wohooo!, Thanks.

venkatesh-appoids commented 4 years ago

@yasoza for me also not working , help is appreciated

andreyvital commented 4 years ago

@venkatesh-appoids looks like now you have to submit a request for Google to allow you to use this permission.