SMS verification with Android SmsRetrieverClient not parsing message

元气小坏坏 提交于 2019-12-10 20:51:14

问题


Tried to implement SMS auto-reading for verification following the steps here: https://developers.google.com/identity/sms-retriever/request

1) declared

lateinit var smsRetrieverClient: SmsRetrieverClient
private lateinit var smsReceiver: SmsBrReceiver

2) Initialised and registered them in login 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) Made BroadcastReceiver for SmsRetriever

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) The SMS comes in format

\u200b\u200bPlease enter code: 1111 /appKeyXf56

And the app key at the end of SMS matches what the installed app signing key gives for generation as described here: https://developers.google.com/identity/sms-retriever/verify#computing_your_apps_hash_string

CommonStatusCodes.SUCCESS does not get called after SMS is received, but CommonStatusCodes.TIMEOUT does get called after 5 minutes.

What is missing? Just in case gradle declarations for phone auth are:

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

回答1:


The solution was to shorten the message, although it seemed to not be over 140 bytes. After making the SMS text to ~30 characters auto-read of SMS works without other changes.



来源:https://stackoverflow.com/questions/53487081/sms-verification-with-android-smsretrieverclient-not-parsing-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!