SMS Receiver

For use case like one time password, it would be easier to autofill the code to the user for better experience. Using Google SMS Verification API, this codebase will help you to implement that.

Before that, keep in mind that the message in SMS must:

  • Be longer than 140 bytes
  • Contain one-time code (in this case, one time password)
  • Include 11 characters hash that identifies the app,

So the message should be looking something like this:

Your ExampleApp code is: 123ABC78

FA+9qCX9VSu

Help

to get application hash, call getAppHash() from Context and log it

Then, call startSMSReceiver method in Activity or Fragment like this:

override fun initObserver() {
    super.initObserver()
    startSMSReceiver(startMessage = "your code is: ", otpLength = 5) { success, message ->
        if (success)
            displayDialog(this, "OTP Received", message = message, positiveAction = Pair("Okay") { finishActivity() })
        else
            displayToast(this, message)
    }
}