1

I only want to get the OTP first. Then I will create an account. But I am unable to get OTP. The phone number format is also correct. Below is my code. Is there any error in it? I think PhoneAuthProvider.getInstance().verifyPhone() is working properly so there will no error in it.

public class second extends AppCompatActivity {

    EditText phoneNumberET, OTPET;
    String phoneNumber, verificationCode;
    FirebaseAuth mAuth;

    PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBack;
    public void submit(View view)
    {
         phoneNumber = phoneNumberET.getText().toString();

        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phoneNumber,
                60,
                TimeUnit.SECONDS,
                TaskExecutors.MAIN_THREAD,
                mCallBack
        );
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        phoneNumberET = (EditText) findViewById(R.id.phoneNumberEditText);
        OTPET = (EditText) findViewById(R.id.OTPEditText);

        mAuth = FirebaseAuth.getInstance();

        mCallBack = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
                Log.i("complete","inside");
            }

            @Override
            public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                super.onCodeSent(s, forceResendingToken);
                verificationCode = s;
            }

            @Override
            public void onVerificationFailed(@NonNull FirebaseException e) {
            }
        };
    }
}
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70
Nikunj Patel
  • 42
  • 1
  • 4

2 Answers2

2

As I know, there are some policies with Firebase Phone Authentication. Example:

  1. If you're trying to login with a same number several times, Firebase Authentication server will automatically block that number by thinking it as a spam number.
  2. Try to login with several numbers in several times. (I know it's painful!)
  3. If you're trying with just one number, you can wait for some time for your next login with that number.

There is a solution for debugging:

You can create some testing phone numbers and verification code for debugging your app with Firebase Phone Authentication. To do this:

  1. Go to your Firebase Console
  2. Go to Authentication
  3. Click on Phone and you'll find Phone numbers for testing (optional)
  4. Add your testing phone number with country code +1. And, also add your verification code for that number.
  5. Click Add then, Save

In this way, you can easily test your app even in Emulator with Firebase Phone Authentication!

Shariful Islam Mubin
  • 2,146
  • 14
  • 23
  • 2
    Thank you so much sir for your solution. It is correct. Later i found that i was entering the number which i was using on the device, so firebase was automatically detecting it. So that was the reason why i was not getting OTP. – Nikunj Patel Dec 04 '19 at 06:50
0

enter image description here

Rumit Patel
  • 8,830
  • 18
  • 51
  • 70