javaandroidgoogle-apisms-verification

Extract OTP from message in Retrivel API(auto SMS verification)


Iam using Google Retriver API for automatic sms verification, now everthings works fine. Iam receiving sms message from Broadcast receiver class. This is my sms message..

<#> Waahan: Your verification code is:1453 jtN03jdhD6p

I want to extract only the otp from the message..

SMSBroadcastReceiver.java

public class SMSBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
            Bundle extras = intent.getExtras();
            Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);

            switch(status.getStatusCode())
            {
                case CommonStatusCodes.SUCCESS:
                    String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);

                    break;

                case CommonStatusCodes.TIMEOUT:

                    break;
            }
        }
    }
}

I have searched, but every one is using telephony.. iam using retrievel API.. Thanks in advance..


Solution

  •  case CommonStatusCodes.SUCCESS:
                    String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
     String OTP = message.substring(message.lastIndexOf(":") + 1);
    

    possible using SubString(Start,End) Function