我使用Firebase手机验证,因为有些手机没有收到OTP验证手机号码。对于集成Firebase Phone身份验证,我只需遵循Firebase文档。是否有任何限制,例如仅适用于高级API,而不适用于较低的API。
这是我的Java代码
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
mVerificationInProgress = false;
if (e instanceof FirebaseAuthInvalidCredentialsException) {
toast("Invalid Phone Number");
finish();
Intent intent = new Intent(MobileNoVerification.this, Login.class);
startActivity(intent);
} else if (e instanceof FirebaseTooManyRequestsException) {
toast("Quota exceeded.");
}
}
@Override
public void onCodeSent(String verificationId, PhoneAuthProvider.ForceResendingToken token) {
mVerificationId = verificationId;
mResendToken = token;
}
};
private void startPhoneNumberVerification(String phoneNumber) {
mAuth.signOut();
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber,
60,
TimeUnit.SECONDS,
this,
mCallbacks);
mVerificationInProgress = true;
}
在 onStart()
中调用此 startPhoneNumberVerificatio 方法private void verifyPhoneNumberWithCode(String verificationId, String code) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, 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()) {
} else {
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
error_dialog("Invalid Code!");
}
}
}
});
}
我的编码中有什么问题,指导我以更好的方式做到这一点。