This tutorial is about automatic SMS verification with SMS Retriever API on Android. We will do our best for you to understand this guide. I hope you will like this blog How to Automatically Check SMS with SMS Retriever API in Android. If your answer is yes, please share after reading this.
Check how to automatically verify SMS with SMS Retriever API in Android
With the SMS Retriever API, you can automatically perform customer verification via SMS in your Android app, without requiring the customer to manually enter verification codes and without asking for additional permissions from the app. The moment you implement automatic SMS verification in your application, the verification flow looks like this.
What is the SMS Recovery API?
SMS Retriever is an API that lets you verify users’ text messages without forcing them to enter verification codes. With this API, you can extract verification codes for your application. This is done without asking for full SMS read permissions. When the user’s device receives a message, Google Play Services checks the hash of the app. It then sends the text message to your application 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
For mobile number verification, you need to implement the client side first. Then on the server side to complete the verification procedure. It usually sends the user’s phone number to the server that performs the verification. The server then sends an OTP (One Time Password) code to the phone number provided. The SMS Retriever API listens for an SMS containing an OTP code. Upon receiving the code, it sends it back to the server to complete the verification process.
Why use SMS AutoRecover API?
- Google removed all apps that used CALL_LOG and READ_SMS permissions. This is because they violated user privacy. This led to apps with these permissions being removed from the Play Store on January 19, 2021.
- It provides a smoother and easier user experience.
Create a new Android studio project
- Create a new Android studio project
Add necessary dependencies
We will use the following.
- Apache Commons: This library will help us extract the code from the SMS message.
- Google Play Services API: This library contains the SMS Retrieval class.
- EventBus: To listen to SMS received from the SMS Retrieval API, we will use a BroadcastReceiver. EventBus is a publisher/subscriber template library. We use it to communicate between our BroadcastReceiver and Activity classes.
Add them to the build.gradle file and sync the project:
Configure the XML layout for our project
We will create an EditText in this section. This edit text will display a unique code obtained from our SMS message.
Sending mobile number to server
In this step, you need to get the user’s phone number from EditText. Submit it to your verification server, which should return the one-time code. Since I don’t have a verification server yet, we won’t use 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 we added in activity_main.xml. To perform SMS verification on a server, see SMS Verification on Server.
Get an instance of SmsRetriverClient
First we will get an instance of SmsRetrieverClient. This is followed by calling the initSmsRetriever instance function and adding onSuccessListener and onFailureListener to the task. We wrap all of this in a function. The above function is called in the onCreate() method. Our API will pass an SmsRetriever.SMS ACTION RETRIEVER attempt to the application. This happens if a device receives a message containing the code. This intent contains the SMS message and background processing status.
To handle this, we’ll create a BroadcastReceiver class: in the onReceive() method, we first check the background processing status of SMS Retriever. We also construct an instance of the RetrievalEvent class. This is the event class that the EventBus will send to our subscriber. The RetrievalEvent class will be a data class. If you’re completely new to EventBus, consider learning more here.
The properties of this data class are set on the retrieved SMS message. This is done if background processing was successful. Typically, a timeout occurs if no message is received within 5 minutes. If this happens, the timeout is set to true. Then send the event to the listening subscriber.
Register the BroadcastReceiver in the Android manifest
In your app’s AndroidManifest.xml file, register BroadcastReceiver: Next, in our MainActivity class, we’ll register, unregister, and implement our subscribers. The onReceiveSms() method will be called when an event is posted. It is usually annotated with the @Subscribe annotation. Registering and unregistering receivers is usually done with the onStart() and onStop() methods respectively. The substringAfterLast() function extracts the code sent by SMS.
Final Words: How to Automatically Verify SMS with SMS Retriever API in Android
Hope you understand this article How to Automatically Check SMS with SMS Retriever API in Android, if your answer is no, you can ask anything via the contact forum section linked to this article. And if your answer is yes, share this article with your family and friends.