jgkiano / react-native-android-sms-verification-api

A wrapper for the android sms verification api. Includes SMS Retriever and SMS User Consent
MIT License
12 stars 6 forks source link

When app is in background app get crashed #14

Open infotheme-dev opened 3 weeks ago

infotheme-dev commented 3 weeks ago

I found a solution for this issue, when you recieve sms and app is in background "using android sms consent api react native" or using firebase recaptcha it's getting crashed

so the solution is

Please correct me if i am wrong


useEffect(() => {
    const handleAppStateChange = (nextAppState) => {
      if (
        appState.current.match(/inactive|background/) &&
        nextAppState === 'active'
      ) {
        // App has come to the foreground
        handleOnStartUserConsentMessageListener();

        receiveVerificationSMS ((error, message) => {

            if (error) {
            // handle error
            console.log("SMS CONSENT error",error);
            } else {
            let code = retrieveVerificationCode(message);
            setOtp(code);

            }
        });

        setIsBackground(false);
      } else if (appState.current === 'active' && nextAppState.match(/inactive|background/)) {
        // App has gone to the background
        setIsBackground(true);
      }

      appState.current = nextAppState;
    };

    AppState.addEventListener('change', handleAppStateChange);

    return () => {
      AppState.removeEventListener('change', handleAppStateChange);
    };
  }, []);