在 Firebase 电话身份验证中未接收代码。
Posted
技术标签:
【中文标题】在 Firebase 电话身份验证中未接收代码。【英文标题】:Not Receiving code in Firebase Phone Authentication. 【发布时间】:2017-10-06 13:53:21 【问题描述】:我正在尝试在我的应用中实现 Firebase 电话身份验证。 我在GitHub上引用了firebase android文档,但是我无法通过短信获取代码。
我不知道为什么会这样?我在真机上测试。
另外,我正在向另一个人发送短信,因为真实设备没有 SIM 卡。但我确信这不是问题。这样对吗?
这是我的代码
void logIn()
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks()
@Override
public void onVerificationCompleted(PhoneAuthCredential credential)
// This callback will be invoked in two situations:
// 1 - Instant verification. In some cases the phone number can be instantly
// verified without needing to send or enter a verification code.
// 2 - Auto-retrieval. On some devices Google Play services can automatically
// detect the incoming verification SMS and perform verificaiton without
// user action.
Log.d("verification", "onVerificationCompleted:" + credential);
// [START_EXCLUDE silent]
mVerificationInProgress = false;
// [END_EXCLUDE]
// [START_EXCLUDE silent]
// Update the UI and attempt sign in with the phone credential
// [END_EXCLUDE]
signInWithPhoneAuthCredential(credential);
@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.
Log.w("verification", "onVerificationFailed", e);
// [START_EXCLUDE silent]
mVerificationInProgress = false;
// [END_EXCLUDE]
if (e instanceof FirebaseAuthInvalidCredentialsException)
else if (e instanceof FirebaseTooManyRequestsException)
// The SMS quota for the project has been exceeded
// [START_EXCLUDE]
Snackbar.make(findViewById(android.R.id.content), "Quota exceeded.",
Snackbar.LENGTH_SHORT).show();
// [END_EXCLUDE]
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token)
// 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.
Log.d("verification", "onCodeSent:" + verificationId);
// Save verification ID and resending token so we can use them later
mVerificationId = verificationId;
mResendToken = token;
;
PhoneAuthProvider.getInstance().verifyPhoneNumber(
"+79995198722", // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
mVerificationInProgress = true;
private void verifyPhoneNumberWithCode(String verificationId, String code)
// [START verify_with_code]
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
// [END verify_with_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())
// Sign in success, update UI with the signed-in user's information
Log.d("signIn", "signInWithCredential:success");
FirebaseUser user = task.getResult().getUser();
else
// Sign in failed, display a message and update the UI
Log.w("signIn", "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException)
);
提前致谢!
【问题讨论】:
很好,弗兰克编辑但不是帮手.. ingineer .... 检查号码是否被添加为firebase控制台中的测试号码。如果是这样,您将不会收到短信。 【参考方案1】:我认为您的问题可能是 SHA-1 指纹,因此您需要在 Firebase 控制台中更新 SHA-1 密钥。
步骤:
登录Firebase console后
-
点击您的项目,然后您可以看到您的项目概览
点击 3 个点,然后点击设置
点击添加指纹(底部)
粘贴您的 SHA-1 密钥并点击保存。
SHA-1 密钥的步骤:
-
从左侧面板中选择 Android Studio 中的 gradle
选择您的应用
转到任务 -> android -> 签名报告
然后你可以看到 Sha-1 键。
【讨论】:
解决了我的问题! 很高兴它对您有所帮助。 ;) 嘿@ShylendraMadda,谢谢你,但我有一个问题,我应该将 SHA-1 和 SHA-256 添加到项目设置中吗?添加它们后,我应该构建一个新版本还是旧版本可以工作? @OliverD 是的,您应该添加 SHA-1,无需构建新应用。 在您将应用上传到 Playstore 后,您还需要释放 SHA1 密钥...否则应用也不会发送短信。【参考方案2】:我通过在真机(Android 设备)而不是模拟器上运行应用程序解决了我的问题。
【讨论】:
OP 确实指定他们正在真实设备上进行测试。以上是关于在 Firebase 电话身份验证中未接收代码。的主要内容,如果未能解决你的问题,请参考以下文章