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



我想使用SMS Retriever API自动获取验证码,但我没有从API接收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>

谷歌图书馆:

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

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

您需要包含验证码哈希字符串。请参阅此处了解如何计算哈希字符串。

最新更新