Closed rrahul14 closed 4 years ago
same problem
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" />
same problem! i need help!!!!
same issue listener not working
have you tried everything I have mentioned in my comment.
yes tried all it is not listening.set the permissions in manifest.and got the sample from yours and tried.
does your app asks for the Read SMS permission ?
yes it asked and i gave the permission to access sms related
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 });
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
It worked for me - @yashojha19 It saved a day for me
@yasoza , Wohooo!, Thanks.
@yasoza for me also not working , help is appreciated
@venkatesh-appoids looks like now you have to submit a request for Google to allow you to use this permission.
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