首页 > 解决方案 > 我添加了 OtpBroadcastReceiver 并注册了它,但我似乎无法从中获取消息

问题描述

这是我的片段-

@SuppressLint("HardwareIds")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mPresenter = VerifyOtpPresenter(this)
        androidId = Settings.Secure.getString(activity?.contentResolver, Settings.Secure.ANDROID_ID)
        mPresenter.requestOtp(phoneNumber)

        initClickAndTextListeners()
        initOtpCountdownTimer()


    }

    /**
     * Requesting OTP password for our phone number
     */
    override fun requestOtp(phoneNumber: Long) {
        OtpNetworking.requestOtp(phoneNumber, object : OtpNetworking.RequestOtpCallback {
            override fun onSuccess() {
                Toast.makeText(context, getString(R.string.verify_otp_fragment_sms_arrived), Toast.LENGTH_SHORT).show()
                startSmsRetriever()
            }

            override fun onError(reason: String) {
                Toast.makeText(context, reason, Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun startSmsRetriever() {
        val client = SmsRetriever.getClient(context!!)
        val task = client.startSmsRetriever()
        task.addOnSuccessListener {
            Toast.makeText(context, "Successfully added retriever", Toast.LENGTH_SHORT).show()
        }

        task.addOnFailureListener {
            Toast.makeText(context, "Failed to get SMS", Toast.LENGTH_SHORT).show()
        }
    }

这是我的OtpBroadcastReceiver-

class OtpBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent) {
        Toast.makeText(context, "onReceive", Toast.LENGTH_SHORT).show()
        if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
            val extras = intent.extras
            val status: Status? = extras!![SmsRetriever.EXTRA_STATUS] as Status?
            when (status?.statusCode) {
                CommonStatusCodes.SUCCESS -> {
                    val message: String? = extras[SmsRetriever.EXTRA_SMS_MESSAGE] as String?
                    Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
                }

                CommonStatusCodes.TIMEOUT -> {
                    Toast.makeText(context, "Timeout", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}

和我的清单文件 -


    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="false"
        android:theme="@style/AppTheme">
        <activity android:name=".startup.StartupActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>


        </activity>

        <receiver android:name=".otp.service.OtpBroadcastReceiver" android:exported="true"
            android:permission="com.google.android.gms.auth.api.phone.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/>
            </intent-filter>
        </receiver>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

我似乎无法从我的广播接收器获得任何信息,尽管短信检索器的 toast 消息确实显示成功添加检索器

我想我错过了片段和广播接收器之间的连接,但我不确定 - 有人知道我错过了什么吗?

标签: androidbroadcastreceiversms-retriever-api

解决方案


您可以尝试将一些 onOtpReceived 函数传递到您的 OtpBroadcastReceiver 中,看看是否有帮助。

class OtpBroadcastReceiver(onOtpReceived: (String) -> Unit, onOtpTimeout: () -> Unit) : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent) {

        if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
            val extras = intent.extras
            val status: Status? = extras!![SmsRetriever.EXTRA_STATUS] as Status?
            when (status?.statusCode) {
                CommonStatusCodes.SUCCESS -> {
                    val message: String? = extras[SmsRetriever.EXTRA_SMS_MESSAGE] as String?
                    onOtpReceived(message)
                }

                CommonStatusCodes.TIMEOUT -> {
                   onOtpTimeout()
                }
            }
        }
    }
}

推荐阅读