简体   繁体   English

Firebase 未向手机号码发送 OTP

[英]Firebase is not sending OTP to the Mobile Number

I am facing something different type of problem.我面临着不同类型的问题。 I am using Firebase Mobile number authentication in my App.我在我的应用程序中使用 Firebase 手机号码身份验证。 When I am trying to send OTP to the same mobile number that I am using, the OTP is not sending.当我尝试将 OTP 发送到我正在使用的同一手机号码时,OTP 没有发送。 However, if I am sending OTP to the other mobiles from my mobile OTP is sending.但是,如果我从我的移动 OTP 发送 OTP 到其他手机。 I also found If I send OTP from another mobile to my number OTP is coming.我还发现如果我从另一部手机发送 OTP 到我的号码,OTP 即将到来。 Hence there is no issue of Mobile Number.因此不存在手机号码的问题。 On Debugging I found that this code block is not working在调试时我发现这个代码块不工作

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

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

For other numbers, it is working and verification and forceResendingToken are generating.对于其他号码,它正在工作并且正在生成验证和 forceResendingToken。

If you have added your phone number in testing under firebase(Sign-in method->Phones).如果您在 firebase 下的测试中添加了您的电话号码(登录方法->电话)。 then please remove from there to get otp.然后请从那里删除以获取 otp。

Are you implementing onVerificationCompleted ?您是否正在实施onVerificationCompleted In the following 2 cases, no OTP is sent, as explained in https://firebase.google.com/docs/auth/android/phone-auth :在以下 2 种情况下,不会发送 OTP,如https://firebase.google.com/docs/auth/android/phone-auth 中所述

  • Instant verification: phone number can be instantly verified without the need to send an OTP.即时验证:无需发送OTP即可即时验证电话号码。
  • Auto-retrieval: Google Play Services can automatically detect the OTP without any user action.自动检索:Google Play 服务无需任何用户操作即可自动检测 OTP。

In this case, you will directly get back a PhoneAuthCredential.在这种情况下,您将直接取回 PhoneAuthCredential。

As explained already, The phone number is getting verified automatically either by如前所述,电话号码正在通过以下方式自动验证

  1. Instant Verification or即时验证或
  2. Auto Retrieval自动检索

which is why onCodeSent() isn't working.这就是onCodeSent()不起作用的原因。

Now, In order to get rid of the problem, you will need to implement onVerificationCompleted() , most chances are that you have implemented it already but you don't know what put inside.现在,为了摆脱这个问题,您需要实现onVerificationCompleted() ,大多数情况下您已经实现了它,但您不知道里面放了什么。

if you are inside onVerificationCompleted(), then you may not have the OTP with you but you definitely have phoneAuthCredential , you just need to pass this variable to mAuth.signInWithCredential() and that should lead to you to your desired output.如果您在 onVerificationCompleted() 中,那么您可能没有 OTP,但您肯定有phoneAuthCredential ,您只需将此变量传递给mAuth.signInWithCredential() ,这应该会导致您获得所需的输出。

sample code for :示例代码:

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

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

            signInWithCredential(phoneAuthCredential);

        }

sample code for :示例代码:

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

                        }
                    }
                }
            });
}

Update the SHA1 fingerprint.更新 SHA1 指纹。 It worked for me.它对我有用。

Worked for me .为我工作。

Sometimes Google is unable to verify the request coming from you app.有时 Google 无法验证来自您的应用的请求。 In order to solve it.为了解决它。 Re-generate your app's signing report and get your SHA-1 or SHA-256 fingerprints.重新生成您应用的签名报告并获取您的 SHA-1 或 SHA-256 指纹。 Go to your firebase project setting and add those fingerprints.转到您的 firebase 项目设置并添加这些指纹。

How to generate fingerprints 如何生成指纹

For me application id was not updated since I changed my package name and generated a new google-services.json.对我来说,应用程序 id 没有更新,因为我更改了我的包名称并生成了一个新的 google-services.json。

Updating the 'applicationid' solved the issue!更新“applicationid”解决了这个问题!

Use the dependency implementation ' androidx.browser:browser:1.2.0 ' in your build.gradle file.build.gradle文件中使用依赖项实现“ androidx.browser:browser:1.2.0 ”。 Firebase now wants to check verification through images, This dependency will allow to open the browser and the Captcha will be verified and you will get the OTP. Firebase 现在想通过图像检查验证,此依赖项将允许打开浏览器并验证 Captcha,您将获得 OTP。

  1. Update your SHA keys.更新您的 SHA 密钥。 If your app is in Playstore then you will get new SHA keys from Play Console: release---> app intrigity---> SHAH keys.如果您的应用在 Playstore 中,那么您将从 Play 管理中心获得新的 SHA 密钥:发布---> 应用完整性---> SHAH 密钥。 sometimes they updates their website so it will better to search How to get SHA Keys from play console有时他们会更新他们的网站,所以最好搜索 How to get SHA Keys from play console

In my case, I realized that this problem happens with all numbers that has ported from one provider to another.就我而言,我意识到这个问题发生在所有从一个提供商转移到另一个提供商的号码上。 With phone numbers that has never ported Firebase Authentication works like a charm!使用从未移植 Firebase 身份验证的电话号码就像一个魅力!

对于仍在寻找从 Khurshed 推荐的 Google Play Console 获取 SHA 的文档的任何人: Doc

In my case, the received OTP was in the spam.就我而言,收到的 OTP 在垃圾邮件中。

for more information visit欲了解更多信息, 请访问

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM