Xamarin Forms: How to Read SMS and verify OTP automatically in android without using SMS read permission
- Be no longer than 140 bytes
- Begin with the prefix <#>
- One-time Verification code
- Special 11-character hash for you app. That Hash can be generated by the Application. (will explain following steps)
- 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
<?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>
|
using Xamarin.Forms;
namespace ReadOTPXamarinAndroid
{
public static class CommonServices
public static void ListenToSmsRetriever()
DependencyService.Get<IListenToSmsRetriever>()?.ListenToSmsRetriever();
public interface IListenToSmsRetriever
void ListenToSmsRetriever();
}
|
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);
}
|
using System;
namespace ReadOTPXamarinAndroid
{
public enum Events
SmsRecieved,
}
|
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();
}
|
SmsRetrieverClient client = SmsRetriever.GetClient(Application.Context);
|
var task = client.StartSmsRetriever();
|
using Java.Lang;
using ReadOTPXamarinAndroid.Droid;
using Application = Android.App.Application;
[assembly: Dependency(typeof(ListenToSms))]
namespace ReadOTPXamarinAndroid.Droid
{
public class ListenToSms : IListenToSmsRetriever
public void ListenToSmsRetriever()
SmsRetrieverClient client = SmsRetriever.GetClient(Application.Context);
var task = client.StartSmsRetriever();
task.AddOnSuccessListener(new SuccessListener());
task.AddOnFailureListener(new FailureListener());
private class SuccessListener : Object, IOnSuccessListener
public void OnSuccess(Object result)
private class FailureListener : Object, IOnFailureListener
public void OnFailure(Exception e)
}
|
using System.Linq;
using System.Text.RegularExpressions;
using Android.App;
using Android.Content;
using Android.Gms.Common.Apis;
using Com.Google.Android.Gms.Auth.Api.Phone;
using ReadOTPXamarinAndroid;
namespace ReadOTPXamarinAndroid.Droid
{
[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] { SmsRetriever.SmsRetrievedAction })]
public class SmsReceiver : BroadcastReceiver
private static readonly string[] OtpMessageBodyKeywordSet = { "DevEnvExe Generated OTP" }; //You must define your own Keywords
public override void OnReceive(Context context, Intent intent)
try
if (intent.Action != SmsRetriever.SmsRetrievedAction) return;
var bundle = intent.Extras;
if (bundle == null) return;
var status = (Statuses)bundle.Get(SmsRetriever.ExtraStatus);
switch (status.StatusCode)
case CommonStatusCodes.Success:
var message = (string)bundle.Get(SmsRetriever.ExtraSmsMessage);
var foundKeyword = OtpMessageBodyKeywordSet.Any(k => message.Contains(k));
if (!foundKeyword) return;
var code = ExtractNumber(message);
Utilities.Notify(Events.SmsRecieved, code);
break;
case CommonStatusCodes.Timeout:
break;
catch (System.Exception)
// ignored
private static string ExtractNumber(string text)
if (string.IsNullOrEmpty(text)) return "";
var number = Regex.Match(text, @"\d+").Value;
return number;
}
|
using System;
using System.Linq;
using System.Text;
using Android.Content;
using Android.Content.PM;
using Android.Util;
using Java.Security;
using Java.Util;
namespace ReadOTPXamarinAndroid.Droid.Helper
{
public class AppHashKeyHelper
private static string HASH_TYPE = "SHA-256";
private static int NUM_HASHED_BYTES = 9;
private static int NUM_BASE64_CHAR = 11;
/// <summary>
/// Retrieve the app signed package signature
/// known as signed keystore file hex string
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
private static string GetPackageSignature(Context context)
PackageManager packageManager = context.PackageManager;
var signatures = packageManager.GetPackageInfo(context.PackageName, PackageInfoFlags.Signatures).Signatures;
return signatures.First().ToCharsString();
/// <summary>
/// Gets the app hash key.
/// </summary>
/// <returns>The app hash key.</returns>
/// <param name="context">Android app Context.</param>
public static string GetAppHashKey(Context context)
string keystoreHexSignature = GetPackageSignature(context);
String appInfo = context.PackageName + " " + keystoreHexSignature;
try
MessageDigest messageDigest = MessageDigest.GetInstance(HASH_TYPE);
messageDigest.Update(Encoding.UTF8.GetBytes(appInfo));
byte[] hashSignature = messageDigest.Digest();
hashSignature = Arrays.CopyOfRange(hashSignature, 0, NUM_HASHED_BYTES);
String base64Hash = Android.Util.Base64.EncodeToString(hashSignature, Base64Flags.NoPadding | Base64Flags.NoWrap);
base64Hash = base64Hash.Substring(0, NUM_BASE64_CHAR);
return base64Hash;
catch (NoSuchAlgorithmException e)
return null;
}
|
Usually I do not read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Great work admin.Keep update more blog.
ReplyDeleteHire Xamarin Developer
xamarin developer india
Hire Xamarin Mobile Developer
Thanks for sharing!!
ReplyDeleteMsgclub Bulk sms services offer dedicated long code,shared long code and short code services that is highly used by businesses to receive SMS messages.
Nice Post, Thanks for sharing this quality information with us. You Need To visit this site mobile bd about Mobile phone
ReplyDeleteThis is good post. please checkout Samsung Galaxy S11 Price in bangladesh 2020
ReplyDeleteThank you sir. I try it but i faced grade problem. How can solve this?
ReplyDeleteI try this project in Android Studio. but i have faced some problem. Please try to write some android studio project. Thank you.
ReplyDeleteThank you. Automatic sims reading system is working . but some times is not work properly. I am using Bangladeshi Nokia Phone Mobile Price. last check 10.03.2020
ReplyDeleteI just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page! Realme Mobile Price in Bangladesh
ReplyDeleteI have read your article. This is very informative and helpful for me and others. I admire the valuable information you offer in your articles. Thanks for us.Tech largest
ReplyDeleteThere is so much in this article that I would never have thought of on my own. Your content gives readers things to think about in an interesting way.teatv apk download
ReplyDeleteNice article. It's have very important idea. I have a site for mobile price and specification. Please visit...
ReplyDeleteI have read your article. This is very informative and helpful for me and others. I admire the valuable information you offer in your articles. Thanks for us. xiaomi price bangladesh
ReplyDeleteAwesome! Amazing article, I am going to share this blog immediately, it's really useful, Thank you.
ReplyDeletexiaomi price in bangladesh
Nice article. It's have very important idea.
ReplyDelete
ReplyDeleteThank you for sharing the information. Wonderful blog & good post. It’s really helpful for me, waiting for more new posts. Keep Blogging! Bangla Subtitle
great article thanks for share thisarticlee
ReplyDeleteI ‘d mention that most of us visitors are really endowed to exist in a fabulous place with very many wonderful individuals with very helpful things.
ReplyDeletesms verification within seconds
Hi, please How can I identify which sim card have received the SMS?
ReplyDeleteAmazing article. It's very important idea for us. if we follow your working step i think it will be work easily. I have a site for
ReplyDeleteMobile bazar you can also visit here.
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. sms activation
ReplyDelete