华为短信检索器 api 无法正常工作且华为设备

Huawei SMS retriever api is not working and Huawei devices

我正在尝试在华为设备中检索 OTP。但它不起作用。

我已经在华为开发者控制台中创建了符合所有要求的应用程序。

下面是我用来检索短信的代码。

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();
        })
    }

广播接收器

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

这方面的任何帮助都是有益的。

请确认以下几点:

  1. 检查是否开启了接收短信验证码的广播。您可以通过执行断点调试或记录日志来实现。
  2. 检查短信格式是否符合短信自动阅读规则

详情见Docs

检查hash_value字段是否正确

  1. 如果在前面的检查过程中没有出现错误,请您提供完整的日志跟踪,然后我会尝试找出此问题可能存在的问题。 :)

所有代码都运行良好,当我构建发布的 apk 然后密钥库更改时,它在调试模式下运行良好,同时更改了发布模式的哈希值。如果您知道如何在发布模式下 运行,请告诉我们。可能华为开发者账号做了一些改动