java - Firebase 未正确发送 otp

标签 java android

我正在开发一个具有获取 OTP 功能的应用,并且我使用了 Firebase OTP 功能(使用电话号码在 Android 上通过 Firebase 进行身份验证),但不幸的是,有时我遇到了像我这样的问题我没有从 firebase 获取 otp,同时也没有给出任何错误。我不知道我犯了什么错误。需要帮助来解决这个问题,我也尝试过一些可以正常工作的示例,但是当我尝试集成到我的项目中时,firebase 没有发送 otp。我想将手机号码发布到 firestore用户当前的国家/地区代码为 ex(+91,+888)。

这是我迄今为止在项目中尝试过的:

package com.example.NewActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.aayushchaubey.meetdax.R;
import com.example.activity.HomeActivity;
import com.example.activity.PhoneNumberLogin;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.FirebaseTooManyRequestsException;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseAuthSettings;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;

import java.util.concurrent.TimeUnit;


public class LoginWithMobileNumber extends AppCompatActivity {

    EditText phoneNo_Edt;
    Button loginBtn;
    FirebaseAuth firebaseAuth;
    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
    private static final String TAG = "FirebasePhoneNumAuth";

    PhoneAuthProvider.ForceResendingToken mResendToken;
    String mVerificationId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mobileno_verification);

        phoneNo_Edt=(EditText)findViewById(R.id.phoneNoEdt);
        loginBtn=(Button)findViewById(R.id.phoneNoLoginBtn);
        phoneNo_Edt.setSingleLine();
        firebaseAuth= FirebaseAuth.getInstance();

        addOnClickListener();

        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

            @Override
            public void onVerificationCompleted(PhoneAuthCredential credential) {
                // This callback will be invoked in two situations:
                // 1 - Instant verification. In some cases the phone number can be instantly
                //     verified without needing to send or enter a verification code.
                // 2 - Auto-retrieval. On some devices Google Play services can automatically
                //     detect the incoming verification SMS and perform verification without
                //     user action.
                Log.d(TAG, "onVerificationCompleted:" + credential);

                signInWithPhoneAuthCredential(credential);
            }

            @Override
            public void onVerificationFailed(FirebaseException e) {
                // This callback is invoked in an invalid request for verification is made,
                // for instance if the the phone number format is not valid.
                Log.w(TAG, "onVerificationFailed", e);

                if (e instanceof FirebaseAuthInvalidCredentialsException) {
                    // Invalid request
                    // ...
                } else if (e instanceof FirebaseTooManyRequestsException) {
                    // The SMS quota for the project has been exceeded
                    // ...
                }

                // Show a message and update the UI
                // ...
            }

            @Override
            public void onCodeSent(String verificationId,
                                   PhoneAuthProvider.ForceResendingToken token) {
                // The SMS verification code has been sent to the provided phone number, we
                // now need to ask the user to enter the code and then construct a credential
                // by combining the code with a verification ID.
                Log.d(TAG, "onCodeSent:" + verificationId);

                // Save verification ID and resending token so we can use them later
                mVerificationId = verificationId;
                mResendToken = token;

                // ...
            }
        };


    }

    private void addOnClickListener() {

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendSms();
            }
        });
    }

    public void sendSms(){
        String phoneNumber=phoneNo_Edt.getText().toString();
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneNumber,        // Phone number to verify
                60,                 // Timeout duration
                TimeUnit.SECONDS,   // Unit of timeout
                this,               // Activity (for callback binding)
                mCallbacks);        // OnVerificationStateChangedCallbacks

    }

    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        firebaseAuth.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");
                            FirebaseUser user = task.getResult().getUser();

                            if(mVerificationId!=null){
                                Intent intent = new Intent(getApplicationContext(), VerifyOtp.class);
                                intent.putExtra("verificationcode",mVerificationId);
                                startActivity(intent);                            }

                            // ...
                        } else {
                            // Sign in failed, display a message and update the UI
                            Log.w(TAG, "signInWithCredential:failure", task.getException());
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                                // The verification code entered was invalid
                            }
                        }
                    }
                });
    }
}

最佳答案

我在我的应用程序中使用 Firebase OTP 验证,在花了一周时间解决不同场景后,我了解了以下内容。也许它也会对您的情况有所帮助。

考虑以下场景。

  1. 当您调用PhoneAuthProvider.getInstance().verifyPhoneNumber()时,大多数时候Firebase将发送 OTP,我们的代码将收到 onCodeSent打回来。之后,如果设备收到 OTP,sdk 将 调用onVerificationCompletedonCodeAutoRetrievalTimeOut根据您传入参数的超时值。

  2. (也许这可能是您的情况)有时当您调用PhoneAuthProvider.getInstance().verifyPhoneNumber()时,sdk会调用onCodeSent方法,但设备未收到任何 OTP。同时,sdk会自动验证电话号码并调用onVerificationCompleted直接进行,无需发送任何 OTP。

所以你应该考虑上述情况并做出相应的决定。

我仍然不确定这个答案是否能满足您的情况,但它肯定会帮助您更好地理解事情。

关于java - Firebase 未正确发送 otp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50772801/

相关文章:

android - 如何在新 Activity 中使用点击的图片?

android - 带有 Google 游戏服务的多人游戏变体

android - NotificationManager 的多重通知

java - 如何强制转换以运行父类(super class)方法

java - 进程运行时在窗口中显示计时器

java - 为什么我需要排除 "spring-boot-starter-logging"才能使用 spring-geode?

java - HTTP 1.1 流水线

java - Shiro 什么时候调用 doGetAuthorizationInfo 方法?

android - 在 ConstraintLayout 中的 ViewPager 中使用 RecyclerView

java - android 日期比较