gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:11:49 26 4
gpt4 key购买 nike

我想使用 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());
}
}
}

list 文件:
<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

您需要包含验证码和哈希字符串。见 here关于如何计算哈希字符串。

关于java - 尽管设备已收到消息,但未从 SMS 检索器 API 接收 SMS 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61301302/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com