Shacker
Shacker

Reputation: 21

How to detect SMS messages in "Ionic Capacitor" without using the code "getAppHash()" of the sms retriever

I am working with "Ionic capacitor", I need to receive the SMS message coming from a server with a validation code. I only found the "sms-retriever" plugin but it only works by sending the code that generates this.smsRetriever.getAppHas(); Does anyone know another way to detect the arrival of SMS messages?

Upvotes: 2

Views: 966

Answers (1)

SAHIL SHARMA
SAHIL SHARMA

Reputation: 1

Hope this helps !!

// Function to start the SMS receiver for automatically fetching OTP
  const startSmsReceiver = async () => {
    try {
      // Show loader modal while starting SMS receiver
      setShowLoaderModal(true);
      // Start the SMS receiver and get registration status
      const register = await SmsRetriever.startSmsReceiver();
      // Get the app signature hash required for SMS retriever
      const hash = await SmsRetriever.getAppSignature();
      // Add listener for the "onSmsReceive" event to handle incoming messages
      const listener = await SmsRetriever.addListener(
        "onSmsReceive",
        async (receivedMessage) => {
          // Extract OTP from received message using findOtp function
          const otp = findOtp(receivedMessage.message);
          // Set the OTP value in component state
          setOtpValue(otp);
          // Dispatch action to set OTP value in Redux state
          dispatch(verifyOtpActions.setOTPValue(otp));
          // Hide loader modal after OTP retrieval
          setShowLoaderModal(false);
        }
      );
    } catch (error) {
      // If error occurs, hide loader modal, show error toast, and log error to console
      setShowLoaderModal(false);
      // showErrorToast("top", "Unable to auto fetch otp", "error");
      console.error("sms receiver error: ", error);
    }
  };

Upvotes: 0

Related Questions