Firebase未向手机号码发送OTP

tec*_*hie 7 android firebase firebase-authentication

我正面临着一些不同类型的问题.我在我的应用程序中使用Firebase Mobile编号身份验证.当我尝试将OTP发送到我正在使用的同一个手机号码时,OTP不会发送.但是,如果我从我的移动OTP向其他手机发送OTP正在发送.我还发现如果我从其他手机发送OTP到我的号码OTP即将到来.因此,没有手机号码的问题.在调试时我发现这个代码块不起作用

@Override
    public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
        super.onCodeSent(verificationId, forceResendingToken);

        Log.e(TAG, "onCodeSent: s - " + verificationId + " : t - " + forceResendingToken);
        xVerificationId = verificationId;
    }
Run Code Online (Sandbox Code Playgroud)

对于其他数字,它正在工作,验证和forceResendingToken正在生成.

Rak*_*esh 12

如果您在 firebase 下的测试中添加了您的电话号码(登录方法->电话)。然后请从那里删除以获取 otp。

  • 但情况并非总是如此。我遇到了 OTP 的问题,有些用户没有收到,但有些用户却收到了。 (2认同)

boj*_*eil 5

你在实施onVerificationCompleted吗?在以下两种情况下,不会发送 OTP,如https://firebase.google.com/docs/auth/android/phone-auth 中所述

  • 即时验证:无需发送OTP即可即时验证电话号码。
  • 自动检索:Google Play 服务无需任何用户操作即可自动检测 OTP。

在这种情况下,您将直接取回 PhoneAuthCredential。


小智 5

如前所述,电话号码正在通过以下方式自动验证

  1. 即时验证或
  2. 自动检索

这就是为什么onCodeSent()不起作用。

现在,为了摆脱这个问题,你需要实现onVerificationCompleted(),大多数情况下你已经实现了它,但你不知道里面放了什么。

如果您在 onVerificationCompleted() 中,那么您可能没有 OTP,但您肯定有phoneAuthCredential,您只需要将此变量传递给mAuth.signInWithCredential(),这应该会导致您获得所需的输出。

示例代码:

        @Override
        public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {

            Toast.makeText(getApplicationContext(),"Verifying code Automatically",LENGTH_LONG).show();

            signInWithCredential(phoneAuthCredential);

        }
Run Code Online (Sandbox Code Playgroud)

示例代码:

private void signInWithCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {

                        // Sign in success, update UI with the signed-in user's information
                        // Log.d(TAG, "signInWithCredential:success");


                    } else {
                        // Sign in failed, display a message and update the UI
                        // Log.w(TAG, "signInWithCredential:failure", task.getException());

                        }
                    }
                }
            });
}
Run Code Online (Sandbox Code Playgroud)