Expert: Retrieving SMS Automatically in Xamarin (Android) using Huawei Account Kit
We have seen lot of apps having login feature with OTP verification. It automatically verifies user identity and reduces our efforts in Login. We can also implement this feature in our mobile app using Huawei Account Kit ReadSmsManager service. It automatically reads the SMS without adding the SMS Read permission, verifies the user and improves the user experience.
Let us start with the project configuration part:
Step 1: Follow Integrating Text Embedding in Xamarin(Android) project configuration till Step 6 and enable Account Kit in Step 2.
Step 2: Install Huawei Account Kit NuGet Package.
Step 3: Integrate HMS Core SDK.
Let us start with the implementation part:
Step 1: Create activity_main.xml for UI.
Step 2: Start the ReadSmsManager service inside MainActivity.cs OnCreate() method.
Step 3: Create class OnCompleteListener.cs for ReadSmsManager service for success or failure.
Step 4: Create the BroadcastReceiver which will receive the SMS message.
Step 5: Initialize the BroadcastReceiver inside MainActivity.cs OnCreate() method.
Step 6: Register receiver inside MainActivity.cs OnResume() method.
Step 7: Unregister the receiver inside MainActivity.cs OnPause() method.
Step 8: Get the HashValue of the application using code which will be used for sending SMS.
Now Implementation part done.
There are some set of rules for sending SMS. We need to send SMS in the same format, so that ReadSmsManager service recognizes.
Below is the message format:
“prefix_flag Text_Message XXXXXX hash_value”
prefix_flag : It indicates the prefix of an SMS message, which can be <#>, [#], or \u200b\u200b. \u200b\u200b is invisible Unicode characters.
Text_Message : It can be any message as per your wish.
XXXXXX : It is verification code.
hash_value : It is the unique value generated using your application package name. Using this Hash Value, app retrieves the SMS. You can get this value using Step 8 of implementation part.
Tips and Tricks
- It retrieves the whole text message, so you need to filter the message on your requirement.
- Please double check your hash code value while sending the message, otherwise app will not retrieve the message automatically.
- You can use your mobile to send SMS.
In this article, we have learnt about automatic SMS message retrieving for user verification and login which helps in reducing login efforts with improving great user experience.
Thanks for reading! If you enjoyed this story, please provide Likes and Comments.