5

I'm implementing Android app that use Firebase to authenticate my users,

My problem is that after changing the Gradle dependency from:

implementation 'com.google.firebase:firebase-auth:11.8.0'

To any higher SDK version, My code stop sending SMS authentication messages..

My code:

public class AuthenticationActivity extends AppCompatActivity {
    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
    private EditText phoneNum;
    private PinView verifyCodeET;
    private String mVerificationId;
    private FirebaseAuth mAuth;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authentication);
        mAuth = FirebaseAuth.getInstance();
        Button sendCodeButton = findViewById(R.id.submit1);
        Button verifyCodeButton = findViewById(R.id.submit2);
        phoneNum = findViewById(R.id.phonenumber);
        verifyCodeET = findViewById(R.id.pinView);
        sendCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phoneNumber = phoneNum.getText().toString();
                PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        phoneNumber, // Phone number to verify
                        60, // Timeout duration
                        TimeUnit.SECONDS, // Unit of timeout
                        AuthenticationActivity.this, // Activity (for callback binding)
                        mCallbacks); // OnVerificationStateChangedCallbacks
            }
        });
        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                signInWithPhoneAuthCredential(phoneAuthCredential);
            }
            @Override
            public void onVerificationFailed(FirebaseException e) {
            }
            @Override
            public void onCodeSent(String verificationId,PhoneAuthProvider.ForceResendingToken token) {
                mVerificationId = verificationId;
            }
        };
        verifyCodeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String verificationCode = verifyCodeET.getText().toString();
                boolean success = true;
                PhoneAuthCredential credential = null;
                try {
                    credential = PhoneAuthProvider.getCredential(mVerificationId, verificationCode);
                } catch (Exception e) {
                    // "Something went wrong"
                    success = false;
                }
                if (success) {
                    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
                        } else {
                            // "Invalid verification code"
                        }
                    }
                });
    }
}

This happens for any SDK version available between the current 16.1.0 to 12.0.0,

Once I'm changing back to 11.8.0 everything work as expected...

Any help will be appreciated

.

Edit

With Nouman Ch help I've added printing at onVerificationFailed() and saw the error message:

com.google.firebase.auth.FirebaseAuthInvalidCredentialsException: The format of the phone number provided is incorrect. Please enter the phone number in a format that can be parsed into E.164 format. E.164 phone numbers are written in the format [+][country code][subscriber number including area code]. [ Invalid format. ]

Using the answer from here

I've added to the Gradle file:

implementation 'com.googlecode.libphonenumber:libphonenumber:7.0.4'
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.google.firebase:firebase-auth:16.1.0'

To the Java includes:

import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;

And edit the onClicK() function to be:

@Override
public void onClick(View view) {
    String phoneNumber = phoneNum.getText().toString();
    // Check phoneNumber.length()
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    try {
        PhoneNumber numberProto = phoneUtil.parse(phoneNumber, "US");
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneUtil.format(numberProto, PhoneNumberUtil.PhoneNumberFormat.E164), // Phone number to verify
                60, // Timeout duration
                TimeUnit.SECONDS, // Unit of timeout
                AuthenticationActivity.this, // Activity (for callback binding)
                mCallbacks); // OnVerificationStateChangedCallbacks
    } catch (NumberParseException e) {
        // Wrong format
    }
}

Now everything work with the latest SDK version

Yogev Neumann
  • 2,099
  • 2
  • 13
  • 24

1 Answers1

3

Try e.printStackTrace(); to check which error you are getting. Use bellow code:

 mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                signInWithPhoneAuthCredential(phoneAuthCredential);
            }
            @Override
            public void onVerificationFailed(FirebaseException e) {
              e.printStackTrace();
            }
            @Override
            public void onCodeSent(String verificationId,PhoneAuthProvider.ForceResendingToken token) {
                mVerificationId = verificationId;
            }
        };

Hope this will work for you.

Nouman Ch
  • 4,023
  • 4
  • 29
  • 42