1

I am trying to auto capture OTP using google smsRetrieverAPI (document here ) which captures the OTP from the message text provided message is starting form [#] and ending with app hash string for example NMjIX4ZCb0u

Despite sending correct format of message template the broadcast receiver registered in manifest is never calling onReceive with the message which further can be used to get OTP using any OTP regex.

My code is as follows

Android Manifest

 <receiver
            android:name=".receiver.AutoReadSMSReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
            </intent-filter>
        </receiver>

LoginFragment.kt

smsRetrieverClient?.startSmsRetriever()

AutoReadSMSReceiver

public class AutoReadSMSReceiver extends BroadcastReceiver {

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

        String message = null;
        Log.d("AutoReadSms", "onReceive: intent "+intent);
        if (null != intent && null != intent.getExtras() && SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
            Bundle extras = intent.getExtras();
            Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
            if (null != status) {
                Toast.makeText(context,"status code "+status.getStatusCode(), Toast.LENGTH_SHORT).show();
                switch (status.getStatusCode()) {
                    case CommonStatusCodes.SUCCESS:
                        message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
                        break;
                    case CommonStatusCodes.TIMEOUT:
                        break;
                }
            }
        }
        Intent smsintent = new Intent();
        smsintent.setAction(OTPHandlerFragment.AUTO_READ_SMS_COMMAND);
        smsintent.putExtra(OTPHandlerFragment.MESSAGE, message);
        context.sendBroadcast(smsintent);
    }
}

Tried the above thing but not working.

Kanchan Pal
  • 178
  • 8
  • hi . could u find the solution ? – Liya Jul 04 '23 at 12:30
  • Try adding `android:permission="com.google.android.gms.auth.api.phone.permission.SEND"` permission to the `` element inside the _AndroidManifest.xml_. This can be seen at the same link you referenced in the question: https://developers.google.com/identity/sms-retriever/request#4_receive_verification_messages – mr. Gauss Jul 14 '23 at 12:33
  • Yes @Liya It was a bug in Android 13 only to not able to send the sms and retriever API is broken. Will post the answer for the same. – Kanchan Pal Jul 22 '23 at 11:36

1 Answers1

0

The reason for auto otp capture not working is smsRetrieval API fails to start successfully in Android 13 with few non google devices. It gives error in real device that

"Device doesn't support the messaging feature"

This issue is reported to google and being tracked here https://issuetracker.google.com/issues/275036593?pli=1

Kanchan Pal
  • 178
  • 8