广播接收器未接收来自Google SMS检索器API的SMS



我正在尝试从谷歌短信检索器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中的日志,但没有在广播接收器中。请帮帮我。我正在牛轧糖上试用这个程序,但希望兼容所有版本。

根据文档,SMS必须包括应用程序的哈希,该哈希是根据应用程序的包名和用于签名的公共证书生成的。哈希生成在此处进行描述,该网站摘要:

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

所以短信应该是这样的:12346是应用程序FA+9qCX9VSu的OTP

最新更新