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:
- A user initiates the request for the verification of the mobile number to your server.
- Now the server processes the number and checks for its existence.
- The server sends the message to the mobile number. The message contains the verification code and the hash code that identify your app.
- Your device receives the message and Google play services determine the message for your application by using the hash code. Google play services provide the message content to your application
- Your application parse the message content and get the verification code from the message content.
- verification code is now shared with your server for verification.
- The server verifies the code and does the needful.
Initial setup
Add the Gradle dependency to your app/module build.gradle file like below:
1 2 |
api 'com.google.android.gms:play-services-auth:20.2.0' api 'com.google.android.gms:play-services-auth-api-phone:18.0.1' |
Start SMS retriever
Start the SMS retriever by the SMSRetriever client like below:
1 2 3 4 5 6 7 8 9 10 11 12 |
private fun setupSMSRetriever() { val client = SmsRetriever.getClient(mFragmentContext.activity!!) val task = client.startSmsRetriever() task.addOnSuccessListener { Log.d("DEBUG", "SMS retriever is working now") MySmsListener.bindListener(mFragmentContext!!) } task.addOnFailureListener { Log.d("DEBUG", "SMS retriever is not working") } } |
Get verification message
Our MySmsListener class is extending the BroadcastReceiver and inside the onReceive we are receiving the SMS contents.
12345678910111213141516171819202122232425262728293031323334 class MySmsListener : BroadcastReceiver() {override fun onReceive(context: Context, intent: Intent) {if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {val extras = intent.extrasif (extras != null) {val status = extras.get(SmsRetriever.EXTRA_STATUS) as Statuswhen (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? = nullfun bindListener(listener: MessageListener) {mListener = listener}}}
Register the above broadcast receiver with intent filter inside the manifest like below:
1 2 3 4 5 |
<receiver android:name=".helpers.SmsListener" android:exported="true"> <intent-filter> <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/> </intent-filter> </receiver> |
Also we have created MessageListener for sending the message data to our activity like below:
1 2 3 |
public interface MessageListener { void messageReceived(String message); } |
As we are using the messageListener interface to pass data to our activity you need to extend the interface like below:
1 |
class LoginActivity : AppCompatActivity(), MessageListener {} |
Once we have extended the interface now override the messageReceived() like below:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
override fun messageReceived(message: String?) { if (message != null) { var otpRegx = "" for (i in 0 until AppSharedPref.getTwilioOtpLength(context!!)!!.toInt()) { otpRegx += "\\d" } val m = Pattern.compile(otpRegx).matcher(message) while (m.find()) { mContentViewBinding.otpEt.setText(m.group().trim()) break } } } |
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:
1 2 3 |
Your OTP For Login : 1534 w34CwYs+zyo |
Get SMS retrieval hash
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.
Conclusion:
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.