Auto Fetch OTP

1.add below dependencies in gradle file

implementation 'com.google.android.gms:play-services-auth:18.1.0'

implementation 'com.google.android.gms:play-services-auth-api-phone:17.5.0'


2.Create one broadcast receiver, like below

public class SmsBroadcastReceiver extends BroadcastReceiver {

   public SmsBroadcastReceiverListener smsBroadcastReceiverListener;

   @Override

   public void onReceive(Context context, Intent intent) {

       if (intent.getAction() == SmsRetriever.SMS_RETRIEVED_ACTION) {

           Bundle extras = intent.getExtras();

           Status smsRetrieverStatus = (Status) extras.get(SmsRetriever.EXTRA_STATUS);

           switch (smsRetrieverStatus.getStatusCode()) {

               case CommonStatusCodes.SUCCESS:

                   Intent messageIntent = extras.getParcelable(SmsRetriever.EXTRA_CONSENT_INTENT);

                   smsBroadcastReceiverListener.onSuccess(messageIntent);

                   break;

               case CommonStatusCodes.TIMEOUT:

                   smsBroadcastReceiverListener.onFailure();

                   break;

           }

       }

   }


   public interface SmsBroadcastReceiverListener {

       void onSuccess(Intent intent);


       void onFailure();

   }

}


3.In OTP Activity(where we want to enter otp) add below code

private void startSmsUserConsent() {

       SmsRetrieverClient client = SmsRetriever.getClient(this);

       //We can add sender phone number or leave it blank

       // I'm adding null here

       client.startSmsUserConsent(null).addOnSuccessListener(aVoid -> {

            // Toast.makeText(getApplicationContext(), "On Success", Toast.LENGTH_LONG).show();

       }).addOnFailureListener(e -> {

            // Toast.makeText(getApplicationContext(), "On OnFailure", Toast.LENGTH_LONG).show();

       });

   }


   @Override

   protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

       super.onActivityResult(requestCode, resultCode, data);

       if (requestCode == REQ_USER_CONSENT) {

           if ((resultCode == RESULT_OK) && (data != null)) {

               //That gives all message to us.

               // We need to get the code from inside with regex

               String message = data.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE);

             // Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();

               getOtpFromMessage(message);

           }

       }

   }


   private void getOtpFromMessage(String message) {//in this method will get otp

       // This will match any 6 digit number in the message

       Pattern pattern = Pattern.compile("(|^)\\d{6}");

       Matcher matcher = pattern.matcher(message);

       if (matcher.find()) {

           ed1.setText(String.valueOf(matcher.group(0).charAt(0)));

           ed2.setText(String.valueOf(matcher.group(0).charAt(1)));

           ed3.setText(String.valueOf(matcher.group(0).charAt(2)));

           ed4.setText(String.valueOf(matcher.group(0).charAt(3)));

           ed5.setText(String.valueOf(matcher.group(0).charAt(4)));

           ed6.setText(String.valueOf(matcher.group(0).charAt(5)));

       }

   }


   private void registerBroadcastReceiver() {

       smsBroadcastReceiver = new SmsBroadcastReceiver();

       smsBroadcastReceiver.smsBroadcastReceiverListener =

               new SmsBroadcastReceiver.SmsBroadcastReceiverListener() {

                   @Override

                   public void onSuccess(Intent intent) {

                       startActivityForResult(intent, REQ_USER_CONSENT);

                   }


                   @Override

                   public void onFailure() {


                   }

               };

       IntentFilter intentFilter = new IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION);

       registerReceiver(smsBroadcastReceiver, intentFilter);

   }


   @Override

   protected void onStart() {

       super.onStart();

       registerBroadcastReceiver();

   }


   @Override

   protected void onStop() {

       super.onStop();

       Log.d("TAG", "onStop: ");

       unregisterReceiver(smsBroadcastReceiver);

   }

4.call startSmsUserConsent() method in oncreate of OTP Activity

Sign In or Register to comment.