Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not Receiving code in Firebase Phone Authentication.

I'm trying to implement Firebase phone auth in my app. I referred the firebase android documents on GitHub, but I can't get the code by SMS.

I don't know why it is happening? I'm testing it on a real device.

Also, I'm sending SMS to another one because the real device doesn't have a sim card. But I'm sure that it's not a problem. Is it right?

This is my code

 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) {

                        }

                    }
                }
            });
}

Thanks in advance!

like image 527
user8554839 Avatar asked Oct 06 '17 13:10

user8554839


People also ask

How can I authenticate my mobile number?

Phone-based authentication involves sending a one-time password (OTP) to a user over a separate communication channel (e.g. SMS, MMS, WhatsApp, Facebook Messenger, Viber or even voice) from the IP channel (internet) used by the application, providing security in case the IP channel is compromised.

How much OTP is free in Firebase?

Limitations of OTP in firebase: The free plan of firebase has Ten Thousand Verification per month, but if you exceed this limit, you need to pay for that.


1 Answers

I think your problem might be SHA-1 finger print, So you need to update the SHA-1 key in Firebase console.

Steps:

After login to Firebase console

  1. Click on your project then you can see Overview of your project
  2. Click on 3 dots and click Settings
  3. Click Add Finger print (At bottom)
  4. Paste your SHA-1 key click on Save.

Steps to SHA-1 key:

  1. Select gradle in Android Studio from left panel
  2. Select your App
  3. Go to Tasks -> android -> signingReport

then you can see the Sha-1 key.

enter image description here

like image 132
Shailendra Madda Avatar answered Nov 10 '22 02:11

Shailendra Madda