banner image

Auto Read OTP from Device in Xamarin Forms

In this article you will learn,how to implement Auto Retrieve OTP from mobile in our Xamarin Forms Application.Now a days most of  mobile application uses OTP registration for users.So that user can type the OTP and access the application.But Its crazy,if the SMS will automatically populate to the user application without typing,In case OTP should be receive on the same device.

In our previous article we learn that how to use SMS server to send the OTP Message to the user mobile.But today we learn,how to retrieve the OTP which is received on user mobile.

By providing the Read_SMS permission to our android manifest file,we can also retrieve the OTP by using LocalBoardcastManager.But the problem is,Google Play will not allow us to without telling them,why we are providing the Read_SMS permission to the user for read the SMS from user mobile.Google Play thinks that your application is a SPY Application.So Google will ask few question,why we provide Read_SMS permission.Because now a days all authentication are OTP based.So that any one can steal your OTP Message.For protecting user,Google provides a SMS Retriever API,which works without granting Read_SMS permission from manifest.But we need to follow the SMS Retriever API rules for reading the OTP Message from user device.

OTP message should follow this format:
Syntax: <#> Your_Message_With_OTP HashKey
Example: <#> Your login OTP is 1234 yHjlo+hjRFlp
  1. The message should be start with hash within the brackets.
  2. After that you can add your message body along with OTP.
  3. At the last add a 11 digit hash key.How to generate this key hash,I discuss it below.

 How to implement this Auto OTP Reactivation?Just follow this few steps:

Step 1:
  1. Set a package name of our android project.Right on android project => Properties =>Android Manifest => Package name
Set a any package name as your wish.Here I set a package name as

Step 2:

1.   Add a package for Google Auth. Right click on android project => Manage Nuget Packages => Browse

And search Xamarin.GooglePlayServices.Auth and install that package.

Step 3:  

In this step we are going generate hash key from android project.So we need a interface to call the android project functions using dependency service.

1. Create a interface on PCL project,and name it IHashService. Define two method inside this interface.First one is for generating the hash key and the second method is for starting the SMS Retriever API.

After defining the methods inside interface we need to implement it  inside our android project.

2. Right click on the android project and add a class.Here I added a class called HashKey_Android . Inside this class inherit the interface and implement this two function.Do not forget add the assembly tag at the top of the class.

Inside GenerateHashKey method we created an instance of the AppHashKeyHelper Class.So we need to create a class inside android project and name it AppHashKeyHelper.Copy this code from below link.

Step 4:

Create a Broadcast Receiver that will responsible for receive the SMS that are provided by google play service. SMS Retriever API cant communicate with our application. SMS Retriever API read the SMS and send this to google play service,and then google play will send a broadcast to our application by the Key Hash.Actually the key hash is the 11 digit encoded string,that are created from the application package name,which we created above on the step no 1.Remember that if the key hash are wrong then our application will not receive that broadcast SMS.

1. Create a broadcast receiver inside android project.Right click on android project => Add => New Item =>Under Visual C# select Android => Broadcast Receiver. And give it any meaningful name.I named it as SMSBroadcastReceiver. And add this following code.This OnReceive override method will called when any SMS received from device.It will provide the message.

2.Inside this OnReceive method at the line number 35 I am using messaging center for sending that message body to our PCL project.Because we are using Xamarin Cross Platform.And this OnReceive method are from android project so we need the transfer that message body to PCL project so that we can extract that OTP from message body.

3. After creating the broadcast receiver class we need to start that receiver service,otherwise it will not work.So open your MainActivity from android project and add this two line inside the OnCreate method last line.

4.To receive the message from messaging center we already subscribe a message center on our OTP verification content page constructor

It will receive the SMS from the messaging center subscribe method and.And here I have written few lines of code for retrieving the OTP from message.After retrieving the OTP from message body then I populate that OTP to our OTP entry text box.

Remember that when OTP message successfully send from SMS server,we need to start the SMS Retriver API using dependency service.

After starting this Retriever API it will listing for SMS,It will wait for 5 minute after it will stop.So when we send the OTP message,we just start that API. 

So this article help you to learn,to implement auto read OTP from device.Next we have to match the OTP is correct or not.So you can set your business logic,how you match the OTP.

Here I am not discussing,how to use OTP timeout,It depends on your business logic depends on your requirement.If you have any query you can comment below.

Auto Read OTP from Device in Xamarin Forms Auto Read OTP from Device in Xamarin Forms Reviewed by SP Tutorials on December 12, 2019 Rating: 5


  1. Below code does not work:
    MessageDigest messageDigest = MessageDigest.GetInstance(HASH_TYPE);

  2. This post is so useful and makes a bit of extremely pleasant data on otp. It is the first occasion when I visit your blog, yet I was amazingly dazzled. Continue posting. oracle fusion scm

  3. When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your. buy tiktok followers

  4. A very excellent blog post. I am thankful for your blog post. I have found a lot of approaches after visiting your post. buy google reviews

  5. Excellent read, Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. 2020 Keystone RV Fuzion 410

  6. It is rather very good, nevertheless glance at the data with this handle. 2021 Winnebago Micro Minnie 2306BHS

  7. how to use MessagingCnter's subscribe in viewmodel class rather than content page class file. can any one help me?

  8. This article is incredibly beneficial and provides some extremely interesting information on otp. Despite the fact that this is my first visit to your blog, I was blown away. Continue to post. Cick here

  9. This article is incredibly beneficial and provides some extremely interesting information on otp. Despite the fact that this is my first visit to your blog, I was blown away. Continue to post. Click here


Powered by Blogger.