javaandroidsmsgoogle-play-servicessms-retriever-api

Not receiving SMS content from the SMS retriever API despite message being received by device


I want to use the SMS Retriever API for automatically getting verification codes, but I'm not receiving SMS content from the API.

I use an emulator for testing, and the SMS is sent to the device correctly, but my program cannot receive and use it.

My SmsReceiver.java class:

public class SmsReceiver extends BroadcastReceiver {

    private static final String TAG = "SmsReceiver";

    @Override public void onReceive(Context context, Intent intent) {
        if(intent == null)
        {
            return;
        }
        Log.e(TAG, "onReceive: ");
        if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
            Bundle extras = intent.getExtras();
            Status mStatus = (Status) extras.get(SmsRetriever.EXTRA_STATUS);

            switch (mStatus.getStatusCode()) {
                case CommonStatusCodes.SUCCESS:
                    String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
                    Log.e(TAG, "onReceive: "+message);
                    break;
                case CommonStatusCodes.TIMEOUT:
                    Log.e(TAG, "onReceive: failure");
                    break;
            }
        }
    }
}

MainActivity.java class

public class MainActivity extends AppCompatActivity{

    private final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        try {
            SmsReceiver smsReceiver = new SmsReceiver();
            IntentFilter filter = new IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION);
            ApplicationLoader.applicationContext.registerReceiver(smsReceiver, filter);

            SmsRetrieverClient client = SmsRetriever.getClient(ApplicationLoader.applicationContext);
            Task<Void> task = client.startSmsRetriever();
            task.addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Toast.makeText(MainActivity.this, "Success", Toast.LENGTH_SHORT).show();
                }
            });
        }
        catch (Exception e)
        {
            Log.e(TAG, e.toString());
        }
    }
}

Manifest file:

<application
    android:name=".ApplicationLoader"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

</application>

Google Libraries:

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

My SMS message:

    <#> Your verify code is: 12345

Solution

  • Your SMS message is missing the hash string: https://developers.google.com/identity/sms-retriever/verify#1_construct_a_verification_message

    You need to include the verification code and the hash string. See here for how to compute the hash string.