Firebase OTP 验证 onVerificationCompleted 未调用

问题描述

我正在尝试设置 OTP 验证,因此当用户输入他们的电话号码时,我向他们发送了一个 PIN 码,然后调用onCodeSent() 并收到了该密码 PIN,但问题是当 {{ 1}} 被调用,我想转到另一个活动,用户可以在其中输入代码 pin 进行验证,但根本没有调用它,我不明白为什么。任何帮助将不胜感激,谢谢。

onVerificationCompleted()

解决方法

onVerificationCompleted() 只会在电话号码经过验证而无需用户输入的情况下被调用。要执行您正在尝试的操作,您应该将您的意图发送到 onCodeSent() 内。

以下是事件的粗略流程(在 documentation 中有详细介绍):

  1. 从用户那里获取电话号码
  2. 致电 PhoneAuthProvider.verifyPhoneNumber(auth)(就像您已经这样做的那样)将 PIN 发送给用户
  3. onCodeSent() 被调用,带有验证 ID 和重新发送的令牌。
  4. onCodeSent() 内,创建一个意图以使用验证 ID 启动“固定输入屏幕”。
  5. 从用户那里获取 PIN 码,然后通过调用 PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId,userInput)
  6. 将其与验证 ID 结合起来
  7. 使用该凭据通过 signInWithCredential(credential) 登录用户。
val auth = PhoneAuthOptions
    .newBuilder(FirebaseAuth.getInstance())
    .setPhoneNumber(phoneNumber)
    .setTimeout(60L,TimeUnit.MILLISECONDS)
    .setActivity(this)
    .setCallbacks(object : PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        override fun onVerificationCompleted(credential: PhoneAuthCredential) {
            // if here,phone number was verified automatically
            mAuth.signInWithCredential(credential)
                 .addOnCompleteListener(/* ... */)
        }

        override fun onVerificationFailed(p0: FirebaseException) {
            Timber.d("Firebase Exception ${p0.message}")
        }

        override fun onCodeSent(verificationId: String,resendToken: PhoneAuthProvider.ForceResendingToken) {
            // if here,code was sent to phone number
            // open pin input screen
            Intent(this,ChangePasswordActivity::class.java).apply {
                putExtra("verificationId",verificationId)
                startActivity(this)
            }
        }

        // we aren't using onCodeAutoRetrievalTimeOut,so it's omitted.
    })
    .build()

PhoneAuthProvider.verifyPhoneNumber(auth)