Table of Contents
This tip is about the how to Automatic SMS verification with SMS Retriever API in Android. So read this free guide, How to Automatic SMS verification with SMS Retriever API in Android step by step. If you have query related to same article you may contact us.
How to Automatic SMS verification with SMS Retriever API in Android – Guide
With the SMS Retriever API, you can perform SMS-based customer verification on your Android app automatically, without requiring the customer to manually enter verification codes and without requiring extra consents from the app. By the time you implement automatic SMS verification in your app, the verification flow looks like this.
What is the SMS Retriever API?
SMS Retriever is an API that allows you to verify users’ SMS without forcing them to enter verification codes. With this API, you can extract verification codes for your app. This is done without asking for full SMS read permissions. When the user’s device receives a message, Google Play Services checks the app’s hash. It then sends the message text to your app via the SMS Retriever API. The app then reads and extracts the code in the SMS message. This code is usually sent back to the server for verification.
SMS verification process
Per mobile number check, you need to implement client side first. Then on the server side to complete the verification procedure. Typically, you send the user phone number for the server performing the check. The server then sends an OTP code (one-time password) to the phone number provided. The SMS Retriever API listens for an SMS containing the OTP code. Upon receiving the code, it sends it back to the server to complete the verification process.
Why use the Automatic SMS Retriever API?
Create a new Android studio project
Add the necessary dependencies
We’re going to use the following.
Add them to the build.gradle file and sync the project:
Configure XML layout for our project
Let’s create edit text in this section. This edit text will display the one-time code obtained from our SMS message.
sending the mobile number for the server
In this step you should get the username phone EditText number. Send it to your verification server, which should return the one-time code. Since I don’t have a verification server yet, we won’t be using this method in this article. We will send the SMS from another phone. The SMS will contain a four-digit code. This code will be extracted and displayed in the EditText that we added in activity_main.xml. To perform SMS verification on a server, see SMS Verification on Server.
Getting an instance of SmsRetriverClient
First, we’ll get an instance of the SmsRetrieverClient. This is followed by invoking the initSmsRetriever instance function and adding onSuccessListener and onFailureListener to the task. We wrap all this in one function. The above function is called in the onCreate() method. Our API will pass a SmsRetriever.SMS RETRIEVED ACTION intent to the application. This happens in case a device receives a message containing the code. This intent keeps the SMS message and processing status in the background.
To handle this, let’s create a BroadcastReceiver class: In the onReceive() method, we first check the status of the SMS Retriever’s background processing. We also construct an instance of the RetrievalEvent class. This is the event class that EventBus will send to our Subscriber. The RetrievalEvent class will be a data class. If you’re completely new to EventBus, consider learning more about it here.
recovery event
The properties of this data class are set for the retrieved SMS message. This is done if background processing was successful. A timeout usually occurs if no messages have been received for 5 minutes. If this occurs, the timeout will be set to true. Then send the event to the listening subscriber.
Register BroadcastReceiver in Android Manifest
In your app’s AndroidManifest.xml file, register BroadcastReceiver: Next, in our MainActivity class, let’s register, unregister, and implement our subscribers. The onReceiveSms() method will be invoked when an event is posted. It is usually annotated with the @Subscribe annotation. Registering and unregistering receivers is usually done in the onStart() and onStop() methods respectively. The substringAfterLast() function is used to extract the code sent by SMS.
Final note
I hope you like the guide How to Automatic SMS verification with SMS Retriever API in Android. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.