Huawei SMS retriever api is not working and Huawei devices

1.3k views Asked by At

I am trying to retrieve OTP in Huawei device. but it is not working.

I have created the app in Huawei developer console with all the requirements.

Below is the code i am using to retrieve the sms.

private fun initSmsManager() {

        val task = ReadSmsManager.start(this@MainActivity)

        task.addOnCompleteListener {

            if (task.isSuccessful) {
                // The service is enabled successfully. Continue with the process.
                Toast.makeText(this, "ReadSms service has been enabled.", Toast.LENGTH_LONG).show()
            } else
                Toast.makeText(this, "The service failed to be enabled.", Toast.LENGTH_LONG).show()
        }
        task.addOnSuccessListener(this, OnSuccessListener {
                if(task.isSuccessful){
                    Toast.makeText(this, "ReadSms service has been enabled.", Toast.LENGTH_LONG).show()
                    myReceiver = MyBroadcastReceiver();
                    val intentFilter = IntentFilter(READ_SMS_BROADCAST_ACTION)
                    registerReceiver(myReceiver, intentFilter)
                }
        })
        task.addOnFailureListener(this, OnFailureListener {
            Toast.makeText(this,it.message,Toast.LENGTH_SHORT).show();
        })
    }

Broadcast receiver

class MyBroadcastReceiver : BroadcastReceiver() {

    companion object {
        val TAG = MyBroadcastReceiver::class.java.simpleName
    }

    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) {

                // Service has timed out and no SMS message that meets the requirement is read. Service ended.
                // doSomethingWhenTimeOut()
            } else if (status?.statusCode == CommonStatusCodes.SUCCESS) {

                if (bundle.containsKey(ReadSmsConstant.EXTRA_SMS_MESSAGE)) {

                    // An SMS message that meets the requirement is read. Service ended.
                    //doSomethingWhenGetMessage(bundle.getString(ReadSmsConstant.EXTRA_SMS_MESSAGE))

                    bundle.getString(ReadSmsConstant.EXTRA_SMS_MESSAGE)?.let {

                        Log.d(TAG, it)

                        val local = Intent()
                        local.action = "service.to.activity.transfer"
                        local.putExtra("sms", it)
                        context!!.sendBroadcast(local)
                    }
                }
            }
        }
    }
}

Any help in this would be beneficial.

2

There are 2 answers

0
zhangxaochen On BEST ANSWER

Please confirm the following points:

  1. Check whether the broadcast for receiving SMS verification codes is enabled. You can do that by performing breakpoint debugging or recording logs.
  2. Check whether the SMS message format meets the rules for automatically reading SMS messages.

For details,See Docs.

enter image description here

Check whether the hash_value field is correct.

  1. If no error occurs during the preceding check, could you pls provide a complete log trace then i will try to find out what can be wrong about this issue. :)
0
AudioBubble On

All code is working fine it works well on debug mode when I build released apk then keystore changed also hash changes for release mode. If you know anything about how to run on release mode , please let us know. Maybe some changes are made in Huawei developer account