Implementing SMS Verification with Huawei SMS Retriever

Ertug Sagman
Huawei Developers
Published in
3 min readJan 26, 2021

Hi everyone! Today I will briefly walkthrough you about how to implement an SMS Retriever system using Huawei’s SMSRetriever service.

First of all, let’s start with why do we need this service in our apps. Applications that involve user verification usually do this with an SMS code, and this SMS code is entered to a field in the app to verify user account. These sms codes are named as One-Time-Password, OTP for short. OTP’s can be used for verification of real user or increasing account security. Our purpose here is to retrieve this code and automatically enter it to the necessary field in our app. So let’s begin!

Our steps through the guide will be as:

1 — Require SMS read permission

2 — Initiate ReadSmsManager

3 — Register broadcast receiver for SMS

4 — Send SMS for user

5 — Register broadcast receiver for OTP — to extract code from SMS —

6 — Create SMSBroadcastReceiver class

1 — We begin by adding our required permission to the AndroidManifest.xml

Note: Don’t forget to check if user has given permission.

2 — Then we add the ReadSmsManager to initiate our SMS service.

Before ReadSmsManager code snippet, we have to add Huawei Account Kit implementation.

After Gradle sync, we can add the SMS manager to our class.

3 — For SMS broadcast receiver, we need to add this little code block.

Note: After you add this code block, you will get an error saying SmsBroadcastReceiver cannot be found, because we didn’t define it yet. We are just getting our SmsActivity ready. We will be adding it once we are done here.

4 — After we register our receiver, we can send the SMS to our user.

Note: here otp will cause an error as it is not defined anywhere yet. You should implement a random OTP generator fitting your likings and assign the value to otp .

5 — Now that we sent an SMS to user, we should register the broadcast receiver to be able to retrieve the code from it.

Note: Once we complete our classes, you should be setting your otp value to your view. This part is left out in the code snippet as view bindings and data bindings may vary on projects.

6 —Finally, where we get to read the messages and find our code.

So that’s it. You should be able to successfully register for SMS sending and retrieving, then read OTP from content and use in whatever way you like.

Thanks for reading through the guide and I hope it is simple and useful for you. Let me know if you have any suggestions or problems.

References

--

--