react-native-otp-verify
react-native-otp-verify copied to clipboard
App Crash after Receiving SMS
I'm using react native version 0.67. My app is getting crashed and closed after I'm receiving my SMS. I even console log the output. The SMS is received but app is crashing. Please give me a fix for this.
Hi @Vignesh1326 please post the code
When received a normal message without HASH we are getting a crash. Please check logs below, i have the package installed in app but we are not using the package anymore.
Fatal Exception: java.lang.RuntimeException: Error receiving broadcast Intent { act=com.google.android.gms.auth.api.phone.SMS_RETRIEVED flg=0x200010 pkg=co.tslc.cashe.android (has extras) } in com.faizal.OtpVerify.OtpBroadcastReceiver@4399d9d at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1629) at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(-.java:2) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:257) at android.app.ActivityThread.main(ActivityThread.java:8246) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1016)
Caused by java.lang.NullPointerException: println needs a message at android.util.Log.println_native(Log.java) at android.util.Log.d(Log.java:158) at com.faizal.OtpVerify.OtpBroadcastReceiver.onReceive(OtpBroadcastReceiver.java:56) at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1619) at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(-.java:2) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:257) at android.app.ActivityThread.main(ActivityThread.java:8246) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1016)
@faizalshap any updates, we are also getting crashes after receiving the sms
@faizalshap, we are also facing the same issue crashes after receiving the sms.. any update ?
let [optfill, setotpfill] = useState('');
useEffect(() => { // RNOtpVerify.getHash() // .then(console.log) // .catch(console.log)
RNOtpVerify.getOtp()
.then((p) => {
RNOtpVerify.addListener(otpHandler)
})
.catch(p => console.log(p));
return () => {
RNOtpVerify.removeListener();
}
}, []);
let otpHandler = (message: string) => {
const msgArr = message.split('\n');
// console.log(msgArr);
if (msgArr[0]) {
code = msgArr[0].split(' ')[5];
setotpfill(code);
}
} This worked for me
I'm using react native version 0.67. My app is getting crashed and closed after I'm receiving my SMS. I even console log the output. The SMS is received but app is crashing. Please give me a fix for this.
i also having same error. please somebody help.
I found that this is caused by a bug in the Google play-services-base
/play-services-basement
packages (ref link). Setting this in app/build.gradle
has prevented this error from appearing again so far (i.e. any version greater than 18.0.0):
implementation 'com.google.android.gms:play-services-base:18.1.0'
implementation 'com.google.android.gms:play-services-basement:18.1.0'
My app was also crashing. I figured it was because of OTP parsing.
I solved it by adding try/catch
const otpHandler = message => { try { const otp = /(\d{6})/g.exec(message)[1]; console.log('otp', otp); setOtp(otp); } catch (e) { console.log('otp not receieved'); } };
But now OTP read is not working most of the times.
i also having same error. please somebody help. RN v0.69.4
I have tested that this is not the issue with the latest changes. Please anyone confirm this and in case you get this issue reopen the issue. Thanks 😊
I'm using this library v1.1.4 and my react native is v0.71.1. I'm facing the same issue as Vignesh1326. I believe the problem occurs when calling listener in this case, RNOtpVerify.addListener(otpHandler). my console.log always return until before addlistener and app crash without any ReactNative error. But in android, it shows the following
D/SMS: SMS I received on my mobile including hash code D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.xenonmobile, PID: 26711 java.lang.RuntimeException: Unable to start receiver com.reactnativeotpautofill.OtpBroadcastReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference at android.app.ActivityThread.handleReceiver(ActivityThread.java:4216) at android.app.ActivityThread.access$1600(ActivityThread.java:261) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2030) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:8163) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference at com.reactnativeotpautofill.OtpBroadcastReceiver.onReceive(OtpBroadcastReceiver.java:39) at android.app.ActivityThread.handleReceiver(ActivityThread.java:4204) at android.app.ActivityThread.access$1600(ActivityThread.java:261) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2030) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:8163) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) I/Process: Sending signal. PID: 26711 SIG: 9
This happens whenever I receive a SMS while listener is listening. If listener is not listening when SMS is sent, there is no issue, but auto fill won't also work as expected. Any solution to fix "Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference?"
@DanDemos Did you find any solution to this issue? Dealing with the same problem since a few days now.
@darshana2797 I think my problem was because I was testing multiple similar libraries and my code messed up with two libraries and something happened. Sorry I forgot the exact reason. But here is the rough basic flow of my working code which is already published.
import OtpInputView from '@twotalltotems/react-native-otp-input'; import RNOtpVerify from 'react-native-otp-verify'; const getOtpNumbers = string => { return string.replace(/[^0-9]/g, '')?.substring(0, 6); }; export const OtpVerificationScreen = props => { const [otp, setOtp] = useState(''); const otpRef = useRef(null); useEffect(() => { if (Platform.OS == 'android') { RNOtpVerify.getHash() RNOtpVerify.getOtp() .then(res => { RNOtpVerify.addListener(otpHandler) }) .catch((err) => { console.log(err) }); } return () => Platform.OS == 'android' && RNOtpVerify.removeListener(); }, []); const otpHandler = (message) => { let otpNum = getOtpNumbers(message) setOtp(otpNum) RNOtpVerify.removeListener() } const onSubmit = () => { // Submit } return ( <> <OtpInputView ref={otpRef} pinCount={6} autoComplete="sms-otp" textContentType={'oneTimeCode'} keyboardType="number-pad" autoFocusOnLoad={false} onCodeChanged={code => setOtp(code)} code={otp} /> <BtnComponent disabled={!otp} onPress={_ => onSubmit()}> Verify </BtnComponent> </> ) }`
I'm still facing this issue. Is it fixed? @Vignesh1326