上载到Play商店后,SMS Retriever API无法正常工作

时间:2019-07-11 07:50:19

标签: android broadcastreceiver android-sms

自动读取SMS释放后不起作用,而当我使用系统的释放模式进行检查时,它的工作正常。在开始时它正在工作,但现在有一段时间它不工作。我不知道解决方法,因为我不知道这个问题。我已经在3个月前实施了该解决方案。

我关注了https://developers.google.com/identity/sms-retriever/request

private void startSMSListener() {
    try {
        SmsRetrieverClient client = SmsRetriever.getClient(this);
        Task<Void> task = client.startSmsRetriever();
        task.addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // API successfully started
                AppLogs.showSOUT("Successfully started");
            }
        });

        task.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // Fail to start API
                AppLogs.showSOUT("Failed started");
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public class SMSReceiver extends BroadcastReceiver {

    private OTPReceiveListener otpListener;

    /**
      * @param otpListener
      */
    public void setOTPListener(OTPReceiveListener otpListener) {
        this.otpListener = otpListener;
    }

    /**
      * @param context
      * @param intent
      */
    @Override
    public void onReceive(Context context, Intent intent) {
        if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction())) {
            Bundle extras = intent.getExtras();
            Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
            AppLogs.showLogD("Status: ", status.toString());
            switch (status.getStatusCode()) {
                case CommonStatusCodes.SUCCESS:

                    //This is the full message
                    String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
                    String[] bhejonaOTP = message.split(":",5);
                //     AppLogs.showSOUT("OTP", bhejonaOTP[0]);
                //     AppLogs.showSOUT("OTP", bhejonaOTP[1].substring(1,5));
                //     AppLogs.showSOUT("OTP", message);
                  /*   <#> Your ExampleApp code is: 123ABC78
                    FA+9qCX9VSu*/

                    //Extract the OTP code and send to the listener

                    if (otpListener != null) {
              //          AppLogs.showSOUT("OTP: ", bhejonaOTP[1].substring(1,5));
                        otpListener.onOTPReceived(bhejonaOTP[1].substring(1,5));
                    }
                    break;
                case CommonStatusCodes.TIMEOUT:
                    // Waiting for SMS timed out (5 minutes)
                    if (otpListener != null) {
                        otpListener.onOTPTimeOut();
                    }
                    break;

                case CommonStatusCodes.API_NOT_CONNECTED:

                    if (otpListener != null) {
                        otpListener.onOTPReceivedError("API NOT CONNECTED");
                    }

                    break;

                case CommonStatusCodes.NETWORK_ERROR:

                    if (otpListener != null) {
                        otpListener.onOTPReceivedError("NETWORK ERROR");
                    }

                    break;

                case CommonStatusCodes.ERROR:

                    if (otpListener != null) {
                        otpListener.onOTPReceivedError("SOME THING WENT WRONG");
                    }

                    break;

            }
        }
    }

    /**
      *
      */
    public interface OTPReceiveListener {

        void onOTPReceived(String otp);

        void onOTPTimeOut();

        void onOTPReceivedError(String error);
    }
}

    <receiver
            android:name=".view.module.authentication.verify.sms.SMSReceiver"
            android:exported="true">
            <intent-filter>
                <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
            </intent-filter>
        </receiver>

0 个答案:

没有答案