Automatic SMS verification in android device

Kanthchandra
4 min readJan 9, 2021

Hi..! Today, we will learn about verify/read SMS automatically during registration, signup…etc; for your android app. Let’s begin with sample

For this, Google introduce two API methods:
1.Automatic SMS verification
2.One-tap SMS verification

refer this link to know more about APIs

In this, I will show how to implement auto SMS verification using first method (Automatic SMS verification).

First add these dependencies as follows in app build.gradle file,

After this, you need to fetch mobile number which is available in your mobile device, for that add getHintMobileNumber()

This method allows to get using mobile number and pop it to screen. Next, you need to add or register receiver for SMS which we get from server or backend.

add receiver in manifest

Initialize your smsbroadcastreceiver and build google API client in your required activity class(onCreate() method). with the help of OtpReceivedInterface call on when activity opens to register your receiver and run the code.

result for getHintMobileNumebr() and select number to continue

After entering number in edittext field or selecting number from getHintMobileNumber(), you need to call startSMSListener()

this smsretreiverclient waits for action which get SMS_RETRIEVED_ACTION in SmsBroadcastReceiver class as we shown above, the message content will get and we need to fetch only OTP and send it to our server/backend for verification. smsretreiverclient will wait for 3 to 5 minutes for SMS which contains our required data/OTP along with 11 digit unique # (hash) string contains alphanumeric string.
That unique 11 digit code which helps you identify your app in mobile

Here “hS3ZQfnaGiV” is the 11-digit unique hash code to identify the app. Now, to generate this 11 digit hash code. we need to create helper class AppSignatureHelper.java” which used to generate 11 digit hash code

Refer this link for helper class to generate 11-digit hash code
-> https://developers.google.com/identity/sms-retriever/verify
->https://github.com/googlearchive/android-credentials/blob/master/sms-verification/android/app/src/main/java/com/google/samples/smartlock/sms_verify/AppSignatureHelper.java

add this class to call appsignature helper to generate hash code

use android :name= “.SmsVerificationApp” in application tag to call this directly or use it in your main activity class (your choice)

with help of SMSverificationApp.java which extends Application you will get app signature ie:11 digit hash code

after generating 11 digit hash code for your app, you can delete/remove SMSverificationApp.java and AppSignatureHelper class files (no need of these two files) and while releasing the app in playstore, need to remove these two java files

Note: for release and debug there should be different 11-digit hash codes. To go live you need to get hash code for release version and for testing the auto verification SMS in your app go for debug or release 11-digit hash code. But for release version debug 11-digit hash code won’t works.

The other method also same as this but need to give permission to read our contacts and messages after getting SMS to read.
https://developers.google.com/identity/sms-retriever/user-consent/overview

See you in next story, Thank you and Happy coding…! :)

Feel free to contact me about this topic…. send your queries and feedback to kanthchandra543@gmail.com using #autosmsverificationMedium

--

--