验证失败(Firebase安卓)

huangapple go评论69阅读模式
英文:

Verification failed(Firebase android)

问题

这是关于使用 Firebase 进行电话号码验证的应用程序。但我一直收到“验证失败”的消息。以下是 OTP 验证的代码:

private void sendVerificationCode(){
    String phoneN =phone.getText().toString();
    
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneN,                           
            60,                  
            TimeUnit.SECONDS,   
            Registration.this,               
            mCallbacks);        
}

PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks= new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
    @Override
    public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
        Toast.makeText(Registration.this,"验证完成",Toast.LENGTH_SHORT).show();
        signInWithPhoneAuthCredential(phoneAuthCredential);
    }

    @Override
    public void onVerificationFailed(FirebaseException e) {
        Toast.makeText(Registration.this,"验证失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
        codeSent=s;
    }
};
private void VerifyCode(){
    String code= otp.getText().toString();
    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
    signInWithPhoneAuthCredential(credential);
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Toast.makeText(getApplicationContext(),"欢迎,现在您可以登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(Registration.this,Login_Reg.class);
                        startActivity(intent);

                    } else {
                        if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            // 输入的验证码无效
                            Toast.makeText(getApplicationContext(),"验证码不正确",Toast.LENGTH_LONG).show();
                        }

                    }
                }
            });
}
}

我一直收到“验证失败”的消息。以下是用户界面(UI)。

验证失败(Firebase安卓)

如何解决这个问题?

英文:

I am trying to make an app for phone number authentication through firebase. But I keep getting the message verification failed.
Here is the code for OTP authentication:-

    private void sendVerificationCode(){
String phoneN =phone.getText().toString();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneN,                           60,                 
TimeUnit.SECONDS,   
Registration.this,               
mCallbacks);        
}
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks= new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
Toast.makeText(Registration.this,&quot;verification completed&quot;,Toast.LENGTH_SHORT).show();
signInWithPhoneAuthCredential(phoneAuthCredential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
Toast.makeText(Registration.this,&quot;verification failed&quot;,Toast.LENGTH_SHORT).show();
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
//super.onCodeSent(s, forceResendingToken);
codeSent=s;
}
};
private void VerifyCode(){
String code= otp.getText().toString();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
signInWithPhoneAuthCredential(credential);
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;() {
@Override
public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(),&quot;Welcome, You can login now&quot;, Toast.LENGTH_LONG).show();
Intent intent = new Intent(Registration.this,Login_Reg.class);
startActivity(intent);
} else {
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
// The verification code entered was invalid
Toast.makeText(getApplicationContext(),&quot;Incorrect Verification Code&quot;,Toast.LENGTH_LONG).show();
}
}
}
});
}
}

I keep getting the message "verification failed".
Below is the UI.

验证失败(Firebase安卓)

How to solve this?

答案1

得分: 0

已经找到解决方案...尝试在实际设备上运行。它会起作用的。

英文:

Got the solution...Try to run it on an actual device. It will work.

huangapple
  • 本文由 发表于 2020年10月16日 14:27:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64383978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定