[英]Firebase phone number authentication code getting null
當我卸載並重新安裝android應用程序時,驗證碼和消息得到完美,但是當我重新啟動應用程序並發送手機號碼進行驗證時,我沒有收到任何消息或驗證碼。 在重新發送按鈕上也是同樣的事情。
我沒有收到任何錯誤消息。
我的代碼是....
private void sendVerificationCode(String mobile) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
mobile,
60,
TimeUnit.SECONDS,
TaskExecutors.MAIN_THREAD,
mCallbacks);
}
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks =
new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential
phoneAuthCredential) {
String code = phoneAuthCredential.getSmsCode();
Log.i("sad","codeis========"+code);
if (code != null) {
edtphoneemail.setText(code);
verifyVerificationCode(code);
}
}
@Override
public void onVerificationFailed(FirebaseException e) {
//Toast.makeText(VerifyPhoneActivity.this, e.getMessage(),
Toast.LENGTH_LONG).show();
Log.i("a","asdf==="+e);
}
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken
forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
Log.i("sadf","sdf=====code sucess fiully"+s);
//storing the verification id that is sent to the user
mVerificationId = s;
mResendToken = forceResendingToken;
}
};
private void verifyVerificationCode(String code) {
//creating the credential
PhoneAuthCredential credential =
PhoneAuthProvider.getCredential(mVerificationId, code);
Log.i("sdf","mVerificationId===="+code);
//signing the user
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()) {
Log.i("sadf","sdf=====code sucess fiully");
} else {
String message = "Somthing is wrong, we will fix it
soon...";
if (task.getException() instanceof
FirebaseAuthInvalidCredentialsException) {
message = "Invalid code entered...";
}
Snackbar snackbar =
Snackbar.make(findViewById(R.id.parent), message,
Snackbar.LENGTH_LONG);
snackbar.setAction("Dismiss", new
View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
snackbar.show();
}
}
});
}
private void resendVerificationCode(String phoneNumber,
PhoneAuthProvider.ForceResendingToken token) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks, // OnVerificationStateChangedCallbacks
token); // ForceResendingToken from callbacks
}
您不會每次都獲得 OTP。 Firebase 會自動檢測手機中的 SIM 卡以及與其關聯的電話號碼。 如果成功,則身份驗證將自動完成,您必須以用戶身份登錄。
我只是將 sim 卡網絡類型 4g 更改為 2g,然后從 firebase 獲取 OTP
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.