androidbroadcastreceiverlistenersms-retriever-api

Sms Retriever API not working because broadcast receiver to auto capture OTP is never called


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.


Solution

  • 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