Android SmsRetrieverClient 未解析消息的 SMS 验证

SMS verification with Android SmsRetrieverClient not parsing message

尝试按照此处的步骤实现短信自动读取验证: https://developers.google.com/identity/sms-retriever/request

1) 已声明

lateinit var smsRetrieverClient: SmsRetrieverClient
private lateinit var smsReceiver: SmsBrReceiver

2) 在登录中初始化并注册它们 Activity onCreate

smsRetrieverClient = SmsRetriever.getClient(this)
smsReceiver = SmsBrReceiver()

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

val task = smsRetrieverClient.startSmsRetriever()
task.addOnSuccessListener(OnSuccessListener<Void> {
    smsReceiver.setTimeout()
})
task.addOnFailureListener(OnFailureListener { e ->
    showCodeInput()
})

3) 为 SmsRetriever 制作 BroadcastReceiver

inner class SmsBrReceiver : BroadcastReceiver() {
    var h = Handler()
    var r: Runnable = Runnable { doTimeout() }

    fun setTimeout() {
        h.postDelayed(r, 600000)
    }

    override fun onReceive(context: Context, intent: Intent?) {

        val action = intent.action

        if (SmsRetriever.SMS_RETRIEVED_ACTION == action) {

            cancelTimeout()
            val extras = intent.extras
            val status = extras!!.get(SmsRetriever.EXTRA_STATUS) as Status
            when (status.statusCode) {
                CommonStatusCodes.SUCCESS -> { // not called

4) 短信格式为

\u200b\u200bPlease enter code: 1111 /appKeyXf56

并且 SMS 末尾的应用程序密钥与安装的应用程序签名密钥为生成提供的匹配,如下所述: https://developers.google.com/identity/sms-retriever/verify#computing_your_apps_hash_string

CommonStatusCodes.SUCCESS 在收到短信后没有被调用,但是 CommonStatusCodes.TIMEOUT 会在 5 分钟后被调用。

少了什么?以防万一 gradle 声明 phone auth 是:

com.google.android.gms:play-services-auth-api-phone:16.0.0
classpath "com.google.gms:google-services:4.2.0

解决方案是缩短消息,虽然它似乎不会超过 140 字节。将 SMS 文本设置为 ~30 个字符后自动读取 SMS 作品,无需其他更改。