Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
In this blog, we going to discuss the SMS retrieval API in android.
SMS retrieval API is used for SMS-based verification without writing the verification code manually.
When we integrate the mobile verification feature in our application and we want to do the verification using the OTP received on our mobile without entering the OTP then this blog is for you.
The above mention verification will work on the below sequence:
Add the Gradle dependency to your app/module build.gradle file like below:
Start the SMS retriever by the SMSRetriever client like below:
Our MySmsListener class is extending the BroadcastReceiver and inside the onReceive we are receiving the SMS contents.
class MySmsListener : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) { val extras = intent.extras if (extras != null) { val status = extras.get(SmsRetriever.EXTRA_STATUS) as Status when (status.statusCode) { CommonStatusCodes.SUCCESS -> { mListener!!.messageReceived(extras.get(SmsRetriever.EXTRA_SMS_MESSAGE) as String) } CommonStatusCodes.TIMEOUT -> { } } } } else { Log.d("SMS: ", intent.action.toString()) } } companion object { private var mListener: MessageListener? = null fun bindListener(listener: MessageListener) { mListener = listener } } } 12345678910111213141516171819202122232425262728293031323334 class MySmsListener : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) { val extras = intent.extras if (extras != null) { val status = extras.get(SmsRetriever.EXTRA_STATUS) as Status when (status.statusCode) { CommonStatusCodes.SUCCESS -> { mListener!!.messageReceived(extras.get(SmsRetriever.EXTRA_SMS_MESSAGE) as String) } CommonStatusCodes.TIMEOUT -> { } } } } else { Log.d("SMS: ", intent.action.toString()) } } companion object { private var mListener: MessageListener? = null fun bindListener(listener: MessageListener) { mListener = listener } }}
Register the above broadcast receiver with intent filter inside the manifest like below:
Also we have created MessageListener for sending the message data to our activity like below:
As we are using the messageListener interface to pass data to our activity you need to extend the interface like below:
Once we have extended the interface now override the messageReceived() like below:
When the data is received update your view accordingly. Also, pass the data to the server for verification.
Once the server receive the mobile number for verification the verification message for the mobile number must have the below format:
For generating the above mentioned hash code(w34CwYs+zyo) for your application so that the play services will determine that the received message is for your app follow the link.
In this blog, we have learned about the implementation of the SMS retrieval API in android.
For more information regarding the SMS retrieval API in android follow the link.
Thanks for reading this blog. You can also check other blogs from here.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Name
Email
Subject
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.