SMS检索器API SMS Broadcaster问题

时间:2019-02-28 18:22:21

标签: android

我正在使用SMS Retriever API来获取OTP,但是我面临的问题是它每次都没有收到SMS。有时会检索SMS内容,有时则什么也没发生。

我已经使用Toast(广播开始了)来显示它是否每次都开始,但是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

1 个答案:

答案 0 :(得分:0)

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