首页 > 解决方案 > SMS Retriever API SMS Broadcaster 问题

问题描述

我过去常常SMS Retriever API得到OTP,但我面临的问题是它不是SMS每次都收到。有时会检索 SMS 内容,有时什么也没有发生。

我已经使用Toast(Broadcaster started)来显示它是否每次都启动但Toast也不是每次都显示。我无法诊断问题。

广播接收器代码:

public class OTPBroadcastReceiver extends BroadcastReceiver {
private String otp;
private static OTPSMSReceiveListner otpsmsReceiveListner = null;
private final Pattern p = Pattern.compile("(|^)\\d{4}");

public static void injectListner(OTPSMSReceiveListner listner){
    otpsmsReceiveListner = listner;
}
@Override
public void onReceive(Context context, Intent intent) {
    try {
        Toast.makeText(context,"Broadcaster started",Toast.LENGTH_LONG).show();
        if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
            Bundle extras = intent.getExtras();
            Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);

            switch (status.getStatusCode()) {
                case CommonStatusCodes.SUCCESS:
                    //Toast.makeText(context,"success",Toast.LENGTH_LONG).show();
                    // Get SMS message contents
                    String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
                    if (message != null) {
                        Matcher m = p.matcher(message);
                        if (m.find()) {
                            otp = m.group(0);
                        }
                        String token;
                        try {
                            token = CommonMethods.getSecurePref("OTP", context);
                        } catch (Exception ex) {
                            token = null;
                        }
                        if (token == null) {
                            //Pass on the text to our listener.
                            otpsmsReceiveListner.onOTPReceived(otp);
                        }
                    }
                    break;
                case CommonStatusCodes.TIMEOUT:
                    Log.d("onReceive", "timed out (5 minutes)");
                    //Toast.makeText(context,"Timeout",Toast.LENGTH_LONG).show();
                    otpsmsReceiveListner.onOTPTimeout();
                    break;
            }
        }
    }
    catch (Exception ex){
        Toast.makeText(context,ex.getLocalizedMessage(),Toast.LENGTH_LONG).show();
    }
}

public interface OTPSMSReceiveListner{
    void onOTPReceived(String otp);
    void onOTPTimeout();
}
}

OTP 类:

SmsRetrieverClient client = SmsRetriever.getClient(mContext);
    Task<Void> task = client.startSmsRetriever();
    task.addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            try
            {
                Log.e("onSuccess","Successfully started retriever");
            }
            catch (Exception ex)
            {
                Log.e("onSuccess",ex.getMessage());
            }
        }
    });

    task.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.e("onFailure", "Failed to start retriever");
        }
    });


    OTPBroadcastReceiver.injectListner(new OTPBroadcastReceiver.OTPSMSReceiveListner() {
        @Override
        public void onOTPReceived(String otp) {
            if(otp.length() == 4) {
                otpField.setText(otp);
                btnVerify.performClick();
            }
        }

        @Override
        public void onOTPTimeout() {
            Log.e("onOTPTimeout","onOTPTimeout");
        }
    });

显现:

<receiver
        android:name=".helpers.OTPBroadcastReceiver"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
        </intent-filter>
    </receiver>

短信:

<#> your App OTP is:8149 585dyDy8cbh

标签: android

解决方案


请参阅此答案https://stackoverflow.com/a/55374780/10449332。请在 SmsRetriever addOnSuccessListener 回调中注册 BroadcastReceiver。


推荐阅读