Introduction
Google is taking user privacy seriously. It has updated its Google Play Developer Policy which restricts SMS and CALL_LOG access. If your app does not require access to Call Log or SMS permissions, you must remove the permissions from your app's manifest.
For apps requesting access to the SMS or Call Log permissions, the intended and permitted uses include default SMS handling, default phone handling, or Assistant handling capability. You should only access Call Log or SMS permissions when your app falls within permitted uses and only to enable your app’s critical core functionality.
An Android App should read the SMS for some specific needs like to verify OTP. The Google Play Service has enabled Android SMS Retriever API that allows you to access the SMS messages and verify it via SMS without device permissions.
Verification Message Format
You need to follow as per Google Policy to construct the verification message and send to the user device, the message should be in the following format.
- No longer than 140 bytes
- Begin with the prefix <#>
- One-time Verification code
- A special 11-character hash for your app. That Hash can be generated by the application (will explain in the following steps).

Now, we have a very good understanding of Google's new policy and message format. In this article, I will explain the following steps. It will help you to integrate Android OTP verification code auto-read using Xamarin.Forms.
- Create New Xamarin Forms Application.
- Add Google Play Services Auth
- Create UI Design
- Dependency Service
- Messaging Center subscribe
- ListenToSms Dependency
- Broadcast Receiver
- Generate Application Hash Key Helper
- Demo Application
Create New Xamarin.Forms Application
In order to implement Auto-Read OTP message, let’s start creating a new Xamarin.Forms project using Visual Studio 2019 or VS for Mac. When accessing Visual Studio 2019 for Mac for the first time, you will come across a new interface for opening and creating the projects.
Open Visual Studio Mac >> Create New Project or select Open recent application.

The available templates will appear on a Mac, like below. Select Xamarin.Forms application with different mobile platforms.

Add Google Play Service NuGet Package
After clicking on the Next button, the Xamarin Project template will generate and load the solutions into the local system. We need to install Xamarin.GooglePlayServices.Auth NuGet package to our Android project for SMS Retriever API.
Right click on Android Project >> Add Nuget Package >> Select or Search “Xamarin.GooglePlayServices.Auth”>> Install the Latest version of Google play service.

Create UI Design
Create a simple UI Design with one Entry and Button Control using Xaml in Xamarin Forms library.
- <?xml version="1.0" encoding="utf-8"?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:ReadOTPXamarinAndroid" x:Class="ReadOTPXamarinAndroid.MainPage">
- <StackLayout Padding="15" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" BackgroundColor="White">
- <Entry PlaceholderColor="Black" Placeholder="Enter OTP" x:Name="smSEntry"></Entry>
- <Button Text="Wait for sms" Clicked="ImageButton_OnClicked"></Button>
- </StackLayout>
- </ContentPage>
The design looks like below,

Dependency Service
DependencyService allows apps to call into platform-specific functionality from shared code. This functionality enables Xamarin.Forms apps to do anything that a native app can do. We need to create an interface design that will define how you interact with platform-specific functionality. Here, reading OTP will support only Android Platform and IOS will support with Autofill so create a DependencyService interface for listening to SMS retriever. Create a new class and interface for CommonServices.
- using Xamarin.Forms;
- namespace ReadOTPXamarinAndroid
- {
- public static class CommonServices
- {
- public static void ListenToSmsRetriever()
- {
- DependencyService.Get<IListenToSmsRetriever>()?.ListenToSmsRetriever();
- }
- }
- public interface IListenToSmsRetriever
- {
- void ListenToSmsRetriever();
- }
- }
Subscribe To Messaging Center
Xamarin.Forms MessagingCenter enables different components to communicate without having to know anything about each other besides a simple Message contract. The MessagingCenter is a static class with Subscribe and Send methods that are used throughout the solution.
Subscribe
Listen for messages with a certain signature and perform some action when they are received. Multiple subscribers can be listening to the same message.
Send
Publish a message for listeners to act upon. If no listeners have subscribed then the message is ignored.
Here, we are creating a utility for subscribing to message listener. It will help when a new message is received.
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Xamarin.Forms;
- namespace ReadOTPXamarinAndroid
- {
- public static class Utilities
- {
- private static readonly object cc = new object();
- public static void Subscribe<TArgs>(this object subscriber, Events eventSubscribed, Action<TArgs> callBack)
- {
- MessagingCenter.Subscribe(subscriber, eventSubscribed.ToString(), new Action<object, TArgs>((e, a) => { callBack(a); }));
- }
- public static void Notify<TArgs>(Events eventNotified, TArgs argument)
- {
- MessagingCenter.Send(cc, eventNotified.ToString(), argument);
- }
- }
- }
Create Enum for message Event type
- using System;
- namespace ReadOTPXamarinAndroid
- {
- public enum Events
- {
- SmsRecieved,
- }
- }
Code Behind Design View
The Code Behind adds the Subscribe message listener and if any message is received from message app with a specific format, it will get notified and read the message and assign the OTP value into the Entry Box.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- using Xamarin.Forms.Xaml;
- namespace ReadOTPXamarinAndroid
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- this.Subscribe<string>(Events.SmsRecieved, code =>
- {
- smSEntry.Text = code;
- });
- }
- private void ImageButton_OnClicked(object sender, EventArgs e)
- {
- CommonServices.ListenToSmsRetriever();
- }
- }
- }


Luis SalasPosted Sep 5, 2021, 2:57 AM
Hello Mr. is it possible to receive a sms code OTP and auto-detect it on a cloned App? for example a Bank app requires the sms otp code be automatically detected, there is no option to input manually and it works on original app, but not in cloned app. I'm willing to pay if you can help me with this issue, thank you.
Hasan ShaddadPosted May 20, 2021, 7:21 AM
@Suthahar Jegatheesan I have a simple question where are you using the SmsReceiver class? I've downloaded the project from github and it launches normally however when I press the wait for sms button the timer doesn't show up and nothing happens. After debugging and tracing the code I figured that the SmsReceiver class isn't called and I guess it's responsible for retrieving the number am I right? Thanks in advance.
Liju DanielPosted Jul 28, 2020, 7:19 AM
Auto read is working with Debug and Release modes perfectly. But after upload a bundle to Playstore this feature is not working with the apk which download from Google Playstore.. Can you help me please......@ Suthahar J
javeed 1357Posted Sep 13, 2019, 10:58 AM
Https://onlinebooking.sand.telangana.gov.in/Masters/Home.aspx. I want this browser Bypass OTP
MOHAMMED ShariqPosted Sep 9, 2019, 12:42 PM
Sir i need a help from you . I am ready to pay the amount. Sir actually the otp which is being sent to my phone should be read by computer
Vijay Svc1Posted Aug 28, 2019, 1:10 AM
This code not working
Logesh PalaniPosted Jul 6, 2019, 1:01 PM
Nice Article :). Thank You