Updated 29 July 2022
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:
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 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") } } |
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 |
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.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.