java - 尽管设备已收到消息,但未从 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
解决方案
您的 SMS 消息缺少哈希字符串:https ://developers.google.com/identity/sms-retriever/verify#1_construct_a_verification_message
您需要包含验证码和哈希字符串。请参阅此处了解如何计算哈希字符串。
推荐阅读
- .net - .NET HotChocolate 泛型类型分页
- android - vkEnumeratePhysicalDevices 在使用 adb shell 执行的 android 命令行程序中返回 0
- vue.js - Vue JS 组件数据没有返回到模板
- python - CDK S3-Assets 参数必须有值
- android - 不支持报警管理器中的其他插件?Flutter 不支持某些任务的后台执行(如果我们杀死了应用程序)?
- babeljs - 如何使用 Babel 要求项目范围之外的文件?
- microsoft-graph-api - 如何使用 Graph Api 检索 OneDrive 中的所有目录和子目录?
- java - ContentCachingRequestWrapper 在过滤器中使用时会产生空输出,但在控制器中使用时不会
- google-data-studio - 将多行单元格中的文本合并为一个(Google Data Studio)
- linux - 如何为从二进制文件安装的 docker 配置 systemctl 服务?