SMS Retriever API在哪里使用11位生成的哈希密钥?

时间:2019-10-11 09:14:38

标签: android

我在服务器端和android端都进行了设置。我的流程正常。我也生成了hashkey,但是现在不知道在哪里使用它。

注意:我删除了RECEIVE_SMS权限,但现在监听器未在呼叫。

代码:

private void startSMSListener() {
    try {
        smsReceiver = new SMSReceiver();
        smsReceiver.setOTPListener(this);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(SmsRetriever.SMS_RETRIEVED_ACTION);
        this.registerReceiver(smsReceiver, intentFilter);

        SmsRetrieverClient client = SmsRetriever.getClient(this);

        Task<Void> task = client.startSmsRetriever();
        task.addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // API successfully started
            }
        });

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

更新:我没有获得OTP: 服务器端代码:

//Prepare Url
                OkHttpClient client=new OkHttpClient();
                Request request = new Request.Builder()
                        .url("http://msg.msgclub.net/rest/services/sendSMS/sendGroupSms?AUTH_KEY="+authKey+"&message=<#>You OTP is:"+randomPIN+"TthI79n9NvR&senderId=REMIND&routeId=1&mobileNos="+mobNo+"&smsContentType=english")
                        .get()
                        .addHeader("Cache-Control", "no-cache")
                        .build();

我应该在onReeive方法中尝试哪种逻辑?谁能指导我? 代码:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.auth.api.phone.SmsRetriever;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.common.api.Status;
import com.tekitsolution.remindly.Dialog.PaymentListDialog;
import com.tekitsolution.remindly.Listener.SMSListener;

public class SMSReceiver extends BroadcastReceiver {
    private static final String TAG = SMSReceiver.class.getSimpleName();

    private SMSListener mListener;

    /**
     * @param mListener
     */
    public void setOTPListener(SMSListener mListener) {
        this.mListener = mListener;
    }


    /**
     * @param context
     * @param intent
     */
    @Override
    public void onReceive(Context context, Intent intent) {
        showLog("Inside SMS Receiver");
        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:

                    //This is the full message
                    String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
                    showLog("message: "+ message);
                    /*<#> Your ExampleApp code is: 123ABC78
                    FA+9qCX9VSu*/

                    //Extract the OTP code and send to the listener

                    if (mListener != null) {
                        mListener.onOTPReceived(message);
                    }
                    break;


                case CommonStatusCodes.API_NOT_CONNECTED:

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

                    break;

                case CommonStatusCodes.NETWORK_ERROR:

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

                    break;

                case CommonStatusCodes.ERROR:

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

                    break;

            }
        }
    }

    private void showLog(String msg) {
        Log.d(TAG, msg);
    }
}

OTP活动:在onCreate中调用了此方法:

 private void startSMSListener() {
        try {
            smsReceiver = new SMSReceiver();
            smsReceiver.setOTPListener(this);

            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(SmsRetriever.SMS_RETRIEVED_ACTION);
            this.registerReceiver(smsReceiver, intentFilter);

            SmsRetrieverClient client = SmsRetriever.getClient(this);

            Task<Void> task = client.startSmsRetriever();
            task.addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                   showLog("success");
                }
            });

            task.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    showLog("failure");
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:0)

  • 您的短信格式应为

<#> SampleApp:您的验证码为143567 QbwSot12oP

1)消息应以<#>开头,表明这是系统的OTP消息。

2)消息应以哈希码结尾,例如QbwSot12oP。

因此,您必须将生成的哈希传递到服务器,并将其附加到此短信的最后。

以下是检索api的完整示例。我用过了,它在我的情况下可以正常工作

  

https://medium.com/android-dev-hacks/autofill-otp-verification-with-latest-sms-retriever-api-73c788636783

在给定的示例中,通过AppSignatureHelper实用地生成哈希密钥。并将其传递到您的服务器,并像我之前展示的那样制作消息格式。