Sms Retriever API不起作用,因为自动捕获OTP的广播接收器从未被调用。

huangapple go评论63阅读模式
英文:

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

 &lt;receiver
            android:name=&quot;.receiver.AutoReadSMSReceiver&quot;
            android:exported=&quot;true&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;com.google.android.gms.auth.api.phone.SMS_RETRIEVED&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/receiver&gt;

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(&quot;AutoReadSms&quot;, &quot;onReceive: intent &quot;+intent);
        if (null != intent &amp;&amp; null != intent.getExtras() &amp;&amp; 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,&quot;status code &quot;+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

huangapple
  • 本文由 发表于 2023年4月11日 01:02:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定