Chapo144
Chapo144

Reputation: 102

SMS Consent API is not reading message from our backend. Is any additional config needed on backend side?

I've integrated the SMS Consent API into my android application to read SMS messages sent from my backend. However, the API isn't detecting messages from the backend. Upon investigation in Android Logs, I noticed that the onRecieve() method of the BroadcastReceiver isn't being called when SMS messages are received from the backend. This suggests there may be an issue with the API or some missing configuration in the backend documentation. Though the docs don't mention anything about backend configuration.

Interestingly, the API successfully detects messages from other well-known senders such as Flipkart, Meesho, Myntra, Zomato, Swiggy, etc.(Like I requested OTP by going to their official websites and trying to log in entering my mobile number).

I followed everything mentioned in docs of SMS Consent API This is the format of the OTP I trigger from Postman. I make sure after the BroadcastReciever has started listening only then I trigger OTP. My OTP SMS Format

In below screenshots you can see that my App can read SMS from other senders. SMS Sent from other Senders like Meesho/Myntra/Flipkart/Zomato/Swiggy is successfully detected

SMS Read successfully from other sender

Backend Configuration: Springboot server hosted on AWS. Am using AWS Pinpoint for sending OTP

My Code:--

private fun startSmsConsentDemo() {

    val task = SmsRetriever.getClient(this@MainActivity).startSmsUserConsent(null)

    val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
    ContextCompat.registerReceiver(
        this@MainActivity,
        smsVerificationReceiver,
        intentFilter,
        ContextCompat.RECEIVER_EXPORTED
    )
}
private val SMS_CONSENT_REQUEST = 2
private val smsVerificationReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        Log.i(TAG, "onReceive() called")
        if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
            val extras = intent.extras
            val smsRetrieverStatus = extras?.get(SmsRetriever.EXTRA_STATUS) as Status

            when (smsRetrieverStatus.statusCode) {
                CommonStatusCodes.SUCCESS -> {
                    val consentIntent =
                        extras.getParcelable<Intent>(SmsRetriever.EXTRA_CONSENT_INTENT)!!
                    try {
                        startActivityForResult(consentIntent, SMS_CONSENT_REQUEST)
                    } catch (e: ActivityNotFoundException) {
                        Log.e(TAG, "onReceive(): error", e)
                    }
                }
                CommonStatusCodes.TIMEOUT -> {
                    Log.w(TAG, "onReceive(): timeout (5 mins over)")
                }
            }
        }
    }
}

public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    Log.i(TAG, "onActivityResult")
    when (requestCode) {
        SMS_CONSENT_REQUEST -> if (resultCode == Activity.RESULT_OK && data != null) {
            // Get SMS message content
            val message = data.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE)
            Log.d(TAG, "Congrats: msg= $message")
        } else {
            // Consent denied. User can type OTC manually.
        }
    }
}

Upvotes: 0

Views: 195

Answers (0)

Related Questions