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

View attachment 5201489

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:

Complete HMS Service implementation

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

View attachment 5201491

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

<uses-permission android:name="android.permission.SEND_SMS" />

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

val permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
if (permissionCheck == PackageManager.PERMISSION_GRANTED)
// Send sms
else
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.SEND_SMS), SEND_SMS_REQUEST_CODE)

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

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

implementation 'com.huawei.hms:hwid:5.1.0.301'

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

val task = ReadSmsManager.startConsent(this@MainActivity, mobileNumber)
task.addOnCompleteListener {
if (task.isSuccessful) {
Toast.makeText(this, "Verification code sent successfully", Toast.LENGTH_LONG).show()
} else
Toast.makeText(this, "Task failed.", Toast.LENGTH_LONG).show()
}

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

val intentFilter = IntentFilter(READ_SMS_BROADCAST_ACTION)
registerReceiver(SmsBroadcastReceiver(), intentFilter)

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.

val smsManager = SmsManager.getDefault()
smsManager.sendTextMessage(
mobileNumber,
null,
"Your verification code is $otp",
null,
null
)

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.

val filter = IntentFilter()
filter.addAction("service.to.activity.transfer")
val otpReceiver = object : BroadcastReceiver() {
override fun onReceive(
context: Context,
intent: Intent
) {
intent.getStringExtra("sms")?.let { data ->
// You should find your otp code here in `data`
}
}
}
registerReceiver(otpReceiver, filter)

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.

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.huawei.hms.common.api.CommonStatusCodes
import com.huawei.hms.support.api.client.Status
import com.huawei.hms.support.sms.common.ReadSmsConstant

class SmsBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val bundle = intent!!.extras
if (bundle != null) {
val status: Status? = bundle.getParcelable(ReadSmsConstant.EXTRA_STATUS)
if (status?.statusCode == CommonStatusCodes.TIMEOUT) {
// Process system timeout
} else if (status?.statusCode == CommonStatusCodes.SUCCESS) {
if (bundle.containsKey(ReadSmsConstant.EXTRA_SMS_MESSAGE)) {
bundle.getString(ReadSmsConstant.EXTRA_SMS_MESSAGE)?.let {
val local = Intent()
local.action = "service.to.activity.transfer"
local.putExtra("sms", it)
context!!.sendBroadcast(local)
}
}
}
}
}
}

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

https://developer.huawei.com/consum...upport-sms-readsmsmanager-0000001050050553-V5

https://developer.huawei.com/consum...Guides/authotize-to-read-sms-0000001061481826

https://medium.com/huawei-developers/android-integrating-your-apps-with-huawei-hms-core-1f1e2a090e98