7

使用 Firebase Auth 进行身份验证时,我想自动输入通过 SMS 接收的代码。我能够接收短信并手动完成身份验证过程,但是当我使用 SmsRetriever 时,应用程序崩溃,然后显示底部工作表对话框。这是出现在 Logcat 中的所有内容:

E/FirebaseAuth:[SmsRetrieverHelper]短信验证码请求失败:未知状态码:17010 null

用户输入电话号码的片段中的代码:

private val SMS_CONSENT_REQUEST = 2  // Set to an unused request code

    private val smsVerificationReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            if (SmsRetriever.SMS_RETRIEVED_ACTION == intent.action) {
                val extras = intent.extras
                val smsRetrieverStatus = extras?.get(SmsRetriever.EXTRA_STATUS) as Status

                when (smsRetrieverStatus.statusCode) {
                    CommonStatusCodes.SUCCESS -> {
                        // Get consent intent
                        val consentIntent = extras.getParcelable<Intent>(SmsRetriever.EXTRA_CONSENT_INTENT)
                        try {
                            // Start activity to show consent dialog to user, activity must be started in
                            // 5 minutes, otherwise you'll receive another TIMEOUT intent
                            startActivityForResult(consentIntent, SMS_CONSENT_REQUEST)
                        } catch (e: ActivityNotFoundException) {
                            // Handle the exception ...
                        }
                    }
                    CommonStatusCodes.TIMEOUT -> {
                        // Time out occurred, handle the error.
                    }
                }
            }
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val task = SmsRetriever.getClient(requireActivity()).startSmsUserConsent(null)
        val intentFilter = IntentFilter(SmsRetriever.SMS_RETRIEVED_ACTION)
        requireActivity().registerReceiver(smsVerificationReceiver, intentFilter)
    }
    
    override fun sendSms() {
        showProgressBar(true)
        SmsRetriever.getClient(requireActivity()).startSmsUserConsent(presenter.getNumber())
        val options = PhoneAuthOptions.newBuilder(auth)
            .setPhoneNumber(presenter.getNumber())
            .setTimeout(58L, TimeUnit.SECONDS)
            .setActivity(requireActivity())
            .setCallbacks(callbacks)
            .build()
        PhoneAuthProvider.verifyPhoneNumber(options)
    }

    override fun onDestroy() {
        super.onDestroy()
        requireContext().unregisterReceiver(smsVerificationReceiver)
    }

这是片段中的代码,用户必须在其中输入代码:

 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            // ...
            SMS_CONSENT_REQUEST ->
                // Obtain the phone number from the result
                if (resultCode == Activity.RESULT_OK && data != null) {
                    // Get SMS message content
                    val message = data.getStringExtra(SmsRetriever.EXTRA_SMS_MESSAGE)
                    // Extract one-time code from the message and complete verification
                    // `message` contains the entire text of the SMS message, so you will need
                    // to parse the string.
                    message?.let { presenter.parseSms(it) }
                    // send one time code to the server
                } else {
                    // Consent denied. User can type OTC manually.
                }
        }
    }
4

2 回答 2

5

打印您的 FirebaseAuthException 错误以查看发生了什么。如果您使用真实电话号码进行开发并一次又一次地使用它,Firebase 可能会暂时阻止该设备。

解决方案:添加一个带有密码的测试电话号码并使用它。

于 2021-03-26T00:41:58.217 回答
1

尝试在 onFailure 中打印异常,例如 --> {p0.message} 打印这一行 logcat,它肯定会显示 --> E/firebase 中的异常:由于异常活动,我们已阻止来自此设备的所有请求。稍后再试。这就是为什么因为我们多次使用这个电话号码登录

于 2021-11-07T16:15:19.123 回答