Search code examples
androidapimobileone-time-password

One tap SMS verification API does not capture incoming SMS with OTP


I am trying to implement the One tap SMS verification API.

I start the sms retriever in on CreateView

private val SMS_CONSENT_REQUEST = 2 


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    // Inflate the layout for this fragment
    val rootView = inflater.inflate(R.layout.fragment_otp, container, false)
    registrationActivity().component.inject(this)

    val task = SmsRetriever.getClient(registrationActivity()).startSmsUserConsent(null)
    task.addOnSuccessListener { 
        Log.d(TAG, "SMS retriever successfully started")
    }
    task.addOnFailureListener {
        Log.e(TAG, "SMS retriever failed to start ${it.message}")
    }



    return rootView
}

this is the broadcast receiver which should launch the consent view

private val smsVerificationReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
            val extras = intent.extras
            val smsRetrieverStatus = extras?.get(SmsRetriever.EXTRA_STATUS) as Status

            when (smsRetrieverStatus.statusCode) {
                CommonStatusCodes.SUCCESS -> {
                    // Get consent intent
                    val consentIntent = extras.getParcelable<Intent>(SmsRetriever.EXTRA_CONSENT_INTENT)
                    try {
                        // Start activity to show consent dialog to user, activity must be started in
                        // 5 minutes, otherwise you'll receive another TIMEOUT intent
                        startActivityForResult(consentIntent, SMS_CONSENT_REQUEST)
                    } catch (e: ActivityNotFoundException) {
                        // Handle the exception ...
                        Log.e(TAG,"something happened ${e.message}")
                    }
                }
                CommonStatusCodes.TIMEOUT -> {
                    Log.d(TAG, "timeout")
                    // Time out occurred, handle the error.
                }
            }
    }
}

and I register the broadcast receiver in onResume

override fun onResume() {
    super.onResume()
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
        activity!!.registerReceiver(smsVerificationReceiver,  intentFilter, SmsRetriever.SEND_PERMISSION,null)
        Log.d(TAG, "registered the sms verification receiver")
    }
}

I am receiving a SMS from a non-contact with the text "Your code is 221-222" but it doesn't get picked up. Any thoughts? Thank you.


Solution

  • According to the docs, a message triggers the broadcast only if it meets these criteria:

    • The message contains a 4-10 character alphanumeric string with at least one number.
    • The message was sent by a phone number that's not in the user's contacts.
    • If you specified the sender's phone number, the message was sent by that number.

    Since your code is 221-222 with a dash, it is not an alphanumeric string, the broadcast is not triggered. Converting your code to 221222 would trigger.