react-native-otp-verify icon indicating copy to clipboard operation
react-native-otp-verify copied to clipboard

App Crash after Receiving SMS

Open Vignesh1326 opened this issue 2 years ago • 9 comments

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.

Vignesh1326 avatar Mar 15 '22 07:03 Vignesh1326

Hi @Vignesh1326 please post the code

faizalshap avatar May 11 '22 05:05 faizalshap

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)

imanshul avatar May 11 '22 10:05 imanshul

@faizalshap any updates, we are also getting crashes after receiving the sms

nikilarigela avatar May 16 '22 05:05 nikilarigela

@faizalshap, we are also facing the same issue crashes after receiving the sms.. any update ?

saiaryan avatar May 16 '22 06:05 saiaryan

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

MAKMoiz23 avatar Jul 14 '22 11:07 MAKMoiz23

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.

inishantsaharan avatar Jul 25 '22 13:07 inishantsaharan

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'

ruibinch avatar Jul 26 '22 02:07 ruibinch

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.

RahulSChauhan50 avatar Aug 03 '22 11:08 RahulSChauhan50

i also having same error. please somebody help. RN v0.69.4

phanvungoc96 avatar Sep 19 '22 03:09 phanvungoc96

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 😊

faizalshap avatar Oct 30 '22 08:10 faizalshap

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 avatar Mar 04 '23 08:03 DanDemos

@DanDemos Did you find any solution to this issue? Dealing with the same problem since a few days now.

darshana2797 avatar May 30 '23 11:05 darshana2797

@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> </> ) }`

DanDemos avatar Jul 24 '23 04:07 DanDemos

I'm still facing this issue. Is it fixed? @Vignesh1326

PraveenPTL avatar Mar 21 '24 12:03 PraveenPTL