2017-10-06 135 views
0

我试图在我的应用中实施Firebase手机身份验证。 我介绍了GitHub上的firebase安卓文档,但我无法通过短信获取代码。未接收Firebase电话身份验证中的代码。

我不知道它为什么会发生?我正在真实设备上测试它。

此外,我发送短信给另一个,因为真正的设备没有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) { 

         } 

        } 
       } 
      }); 
} 
提前

谢谢!

+1

不错,弗兰克编辑但不是帮手.. ingineer .... – Peter

回答

0

我认为您的问题可能是SHA-1指纹,因此您需要更新Firebase控制台中的SHA-1密钥。

步骤:

登录Firebase console

  1. 点击你的项目,你可以看到概述你的项目
  2. 点击3个点,然后单击设置
  3. 单击添加手指后打印(底部)
  4. 粘贴您的SHA-1键点击保存。

步骤SHA-1键:从左侧面板

  • Android Studio中

    1. 选择gradle这个选择你的App
    2. 前往任务 - > Android的 - > signingReport

    然后你可以看到Sha-1键。

    enter image description here