有什么方法可以知道Firebase电话身份验证中的AUTO VERIFICATION是否失败?

时间:2018-06-22 16:40:12

标签: android firebase authentication

Firebase电话认证非常棒。我了解它具有自动验证过程,有时不需要发送otp。但是,有什么方法可以知道自动验证是否失败,然后才提示用户输入OTP。

1 个答案:

答案 0 :(得分:0)

 PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneNumber,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                @Override
                public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

                   if(notAutoRetrieval) {

                        //firebase send otp to the given phone number

                    }else{
                        //firebase does send otp to the given phone number
                    }

                }

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

                    // 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.

                   // if firebase send otp this method is execute 


                    notAutoRetrieval=true;

                }

                @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.

                    if (e instanceof FirebaseAuthInvalidCredentialsException) {
                        // Invalid request
                        Toast.makeText(LogIn.this,e.getLocalizedMessage(),Toast.LENGTH_LONG).show();

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

                    }else{
                        Toast.makeText(LogIn.this,e.getMessage(),Toast.LENGTH_LONG).show();

                    }                      


                }
            });

请参阅此代码。可能对您有帮助。