尽管设备正在接收消息,但仍未从SMS检索器API接收SMS内容

问题描述 投票:1回答:1

我想使用SMS Retriever API自动获取验证码,但是我没有从API接收SMS内容。

我使用仿真器进行测试,并且SMS已正确发送到设备,但是我的程序无法接收和使用它。

我的SmsReceiver.java类:

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类

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());
        }
    }
}

清单文件:

<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库:

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

我的短信:

    <#> Your verify code is: 12345
java android sms smsretriverapi
1个回答
0
投票

您的SMS消息缺少哈希字符串:https://developers.google.com/identity/sms-retriever/verify#1_construct_a_verification_message

您需要包含验证码哈希字符串。有关如何计算哈希字符串的信息,请参见here

© www.soinside.com 2019 - 2024. All rights reserved.