android - Firebase电话号码身份验证码为空
问题描述
当我卸载并重新安装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 卡以及与之关联的电话号码。如果成功,则自动完成身份验证,您必须登录用户。
推荐阅读
- r - 使用 R 从多个 excel 文件中提取特定单元格
- react-native - React Native 错误:带有有效负载 {"name":"GameScreen"} 的操作 'NAVIGATE' 未被任何导航器处理
- javascript - 关闭 iframe 模式时如何停止 youtube 视频
- python - 为什么我无法在 selenium webdriver python 上单击此按钮?
- amazon-s3 - 如何在 Kafka Connect S3 中解析记录头?
- java - Spring-Boot:带参数的注入?
- python - 在客户数据中查找首次购买的月份
- c# - RegistryKey 类中的 ValidateKeyView 有什么意义?
- java - 使用 Proguard 混淆 Spring Boot fat Jar
- google-translate - Google Translate API 会忽略重音字符