广播接收器未从Google SMS检索器API接收SMS

问题描述 投票:1回答:1

我正在尝试从谷歌短信检索器API自动获取OTP,但我的广播接收器中没有任何OTP。这是详细信息:

build.gradle:

implementation "com.google.android.gms:play-services-auth-api-phone:17.3.0"
     implementation 'com.google.android.gms:play-services-auth:17.0.0'
     implementation 'com.google.android.gms:play-services-gcm:17.0.0'
     implementation 'com.google.android.gms:play-services-base:17.0.0'

清单文件:

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

BroadCast接收器类:

class MySMSBroadcastReceiver : BroadcastReceiver() {
    private val TAG = "MySMSBroadcastReceiver"

//    private var otpReceiver: OTPReceiveListener? = null
//
//    fun initOTPListener(receiver: OTPReceiveListener) {
//        this.otpReceiver = receiver
//    }

    override fun onReceive(context: Context, intent: Intent) {
        if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
            val extras = intent.extras
            val status = extras?.get(SmsRetriever.EXTRA_STATUS) as Status
            Log.e("OTP_Message","hdhjvcj")

            when (status.statusCode) {
                CommonStatusCodes.SUCCESS -> {
                    // Get SMS message contents
                    var otp: String = extras.get(SmsRetriever.EXTRA_SMS_MESSAGE) as String
                    Log.e("OTP_Message", otp)
                    // Extract one-time code from the message and complete verification
                    // by sending the code back to your server for SMS authenticity.
                    // But here we are just passing it to MainActivity
//                    if (otpReceiver != null) {
//                        otp = otp.replace("<#> Your ExampleApp code is: ", "").split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0]
//                        otpReceiver!!.onOTPReceived(otp)
//                    }
                }

                CommonStatusCodes.TIMEOUT ->{
                    Log.e(TAG,"TimedOut")
                }
                    // Waiting for SMS timed out (5 minutes)
                    // Handle the error ...
//                    otpReceiver!!.onOTPTimeOut()
            }
        }
    }
}

活动:

val mSmsBroadcastReceiver = MySMSBroadcastReceiver()
val intentFilter = IntentFilter()
intentFilter.addAction(SmsRetriever.SMS_RETRIEVED_ACTION)
applicationContext.registerReceiver(mSmsBroadcastReceiver, intentFilter)

以下启动方法:

private fun startSMSRetrievingProcess(){
        val client = SmsRetriever.getClient(this)
        val task = client.startSmsRetriever()
        task.addOnSuccessListener {
            Log.e(TAG,"SMS received")

            // Successfully started retriever, expect broadcast intent
            // ...
        }

    task.addOnFailureListener {
        Log.e(TAG,"SMS received failure")
        // Failed to start retriever, inspect Exception for more details
        // ...
    }
}

OTP格式:123456是您的应用程序的OTP。

我正在获取task.addOnSuccessListener内部的日志,但未打印在广播接收器内部。请帮帮我。我正在牛轧糖上尝试此程序,但希望兼容所有版本。

android broadcastreceiver sms
1个回答
0
投票

documentation,SMS必须包含您应用的哈希值,该哈希值是由您的应用的程序包名称和用于对其进行签名的公共证书生成的。哈希生成描述为here,该站点的摘要:

The following command computes the hash string from your app's production keystore:

keytool -exportcert -alias PlayDeploymentCert -keystore MyProductionKeys.keystore | xxd -p | tr -d“ [:space:]” | echo -n com.example.myappcat| sha256sum | tr -d“ [:space:]-” | xxd -r -p | base64 |切-c1-11

因此,SMS消息应如下所示:123456是应用程序FA + 9qCX9VSu的OTP

© www.soinside.com 2019 - 2024. All rights reserved.