英文:
Sms Retriever API not working because broadcast receiver to auto capture OTP is never called
问题
我正在尝试使用Google的smsRetrieverAPI(文档在这里)自动捕获OTP,该API从消息文本中捕获以[#]开头并以应用哈希字符串结尾的OTP,例如NMjIX4ZCb0u。
尽管发送了正确格式的消息模板,但清单中注册的广播接收器从未调用onReceive,并进一步可以使用任何OTP正则表达式来获取OTP。
我的代码如下:
Android清单
<receiver
android:name=".receiver.AutoReadSMSReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
</intent-filter>
</receiver>
LoginFragment.kt
smsRetrieverClient?.startSmsRetriever()
AutoReadSMSReceiver
public class AutoReadSMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(@NonNull Context context, @Nullable Intent intent) {
String message = null;
Log.d("AutoReadSms", "onReceive: intent "+intent);
if (null != intent && null != intent.getExtras() && SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
if (null != status) {
Toast.makeText(context,"status code "+status.getStatusCode(), Toast.LENGTH_SHORT).show();
switch (status.getStatusCode()) {
case CommonStatusCodes.SUCCESS:
message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
break;
case CommonStatusCodes.TIMEOUT:
break;
}
}
}
Intent smsintent = new Intent();
smsintent.setAction(OTPHandlerFragment.AUTO_READ_SMS_COMMAND);
smsintent.putExtra(OTPHandlerFragment.MESSAGE, message);
context.sendBroadcast(smsintent);
}
}
尝试了上述内容但不起作用。
英文:
I am trying to auto capture OTP using google smsRetrieverAPI (document here ) which captures the OTP from the message text provided message is starting form [#] and ending with app hash string for example NMjIX4ZCb0u
Despite sending correct format of message template the broadcast receiver registered in manifest is never calling onReceive with the message which further can be used to get OTP using any OTP regex.
My code is as follows
Android Manifest
<receiver
android:name=".receiver.AutoReadSMSReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
</intent-filter>
</receiver>
LoginFragment.kt
smsRetrieverClient?.startSmsRetriever()
AutoReadSMSReceiver
public class AutoReadSMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(@NonNull Context context, @Nullable Intent intent) {
String message = null;
Log.d("AutoReadSms", "onReceive: intent "+intent);
if (null != intent && null != intent.getExtras() && SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
Bundle extras = intent.getExtras();
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
if (null != status) {
Toast.makeText(context,"status code "+status.getStatusCode(), Toast.LENGTH_SHORT).show();
switch (status.getStatusCode()) {
case CommonStatusCodes.SUCCESS:
message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
break;
case CommonStatusCodes.TIMEOUT:
break;
}
}
}
Intent smsintent = new Intent();
smsintent.setAction(OTPHandlerFragment.AUTO_READ_SMS_COMMAND);
smsintent.putExtra(OTPHandlerFragment.MESSAGE, message);
context.sendBroadcast(smsintent);
}
}
Tried the above thing but not working.
答案1
得分: 0
自动 OTP 捕获不起作用的原因是在 Android 13 上,smsRetrieval API 无法成功启动,部分非 Google 设备出现此问题。在真实设备上,会出现以下错误:
"设备不支持消息功能"
此问题已报告给 Google 并正在此处跟踪:https://issuetracker.google.com/issues/275036593?pli=1
英文:
The reason for auto otp capture not working is smsRetrieval API fails to start successfully in Android 13 with few non google devices. It gives error in real device that
"Device doesn't support the messaging feature"
This issue is reported to google and being tracked here https://issuetracker.google.com/issues/275036593?pli=1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论