Implementing SMS Verification with Huawei SMS Retriever

Search This thread

ErtugSagman

Member
Jan 12, 2021
9
15
Hi everyone! Today I will briefly walkthrough you about how to implement an SMS Retriever system using Huawei’s SMSRetriever service.
1_LCeoKUok8X5vfX4RS1FVhA.jpeg


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


0000000000011111111.20210104202408.65377856273615258814303079219332.png


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

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

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

Code:
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.


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

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

Code:
val task = ReadSmsManager.startConsent([email protected], 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.

Code:
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.


Code:
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.


Code:
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.


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
 
Last edited:

ErtugSagman

Member
Jan 12, 2021
9
15
How app detects the OTP from msg application when OTP arrives?
Code:
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)

you should extract your otp here from 'data'.
 

ProManojKumar

Member
Dec 29, 2020
16
2
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:

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

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

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

Code:
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.


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

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

Code:
val task = ReadSmsManager.startConsent([email protected], 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.

Code:
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.


Code:
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.


Code:
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.


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
does it require any charges?
 

Top Liked Posts

  • There are no posts matching your filters.
  • 14
    Hi everyone! Today I will briefly walkthrough you about how to implement an SMS Retriever system using Huawei’s SMSRetriever service.
    1_LCeoKUok8X5vfX4RS1FVhA.jpeg


    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


    0000000000011111111.20210104202408.65377856273615258814303079219332.png


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

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

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

    Code:
    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.


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

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

    Code:
    val task = ReadSmsManager.startConsent([email protected], 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.

    Code:
    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.


    Code:
    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.


    Code:
    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.


    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
    3
    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:

    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

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

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

    Code:
    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.


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

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

    Code:
    val task = ReadSmsManager.startConsent([email protected], 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.

    Code:
    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.


    Code:
    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.


    Code:
    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.


    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
    1
    Does it support text based otp? and does it supports 6 and 8 digit number OTP?
Our Apps
Get our official app!
The best way to access XDA on your phone
Nav Gestures
Add swipe gestures to any Android
One Handed Mode
Eases uses one hand with your phone