Firebase Phone Auth isn't sending SMS with code

14,593

When you add a phone number for testing, you are basically whitelisting it. Whitelisted phone numbers are used for testing phone number authentication without sending an actual SMS message.

No verification code SMS is sent to whitelisted phone numbers and a default OTP of 123456 (unless you have provided the 6-digit verification code for that specific number when adding to the test console). Try to verify using this code, it will authenticate and generate a valid verification ID.

For getting the SMS, remove your number from the whitelist and try again.

Reference: https://firebase.google.com/docs/auth/android/phone-auth#test-with-whitelisted-phone-numbers

Share:
14,593
Marco Fregoso
Author by

Marco Fregoso

Updated on June 29, 2022

Comments

  • Marco Fregoso
    Marco Fregoso almost 2 years

    I'm trying to set up phone number authentication using Firebase for my Android app. I've enabled phone authentication on the "authentication" tab on Firebase console and added my number for testing. I've tried with multiple numbers but I get no SMS sent to any of them despite the console telling me PhoneCodeSent function was fired and displaying a success message. I've tried to fix it for a while and can't seem to find a solid answer. What's wrong with my code?

    My code is the following

    ...
    ...
    ...
    String verificationId;
    int resendingToken;
    
    Future<void> _sendConfirmationCode() async {
    if (formKey.currentState.validate()) {
      formKey.currentState.save();
    
      final PhoneVerificationCompleted verificationCompleted = (FirebaseUser user) {
        setState(() {
          print('verification has been completed');
        });
      };
    
      final PhoneVerificationFailed verificationFailed = (AuthException authException) {
        setState(() {
          print(countrySelected);
          print(this.phone + "\n");
          print('verification failed error: ' + authException.message);}
        );
      };
    
      final PhoneCodeSent codeSent = (String verificationId, [int forceResendingToken]) async {
        this.verificationId = await verificationId;
        this.resendingToken = await forceResendingToken;
        print("code sent: "+ verificationId);
      };
    
      final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
          (String verificationId) {
        this.verificationId = verificationId;
        print("time out");
      };
    
      if (this.phone.isNotEmpty) {
    
        await FirebaseAuth.instance.verifyPhoneNumber(
          phoneNumber: "<PHONE NUMBER HARDCODED HERE>", //I've tried hardcoding my number too but it didn't work
          timeout: const Duration(seconds: 5),
          verificationCompleted: verificationCompleted,
          verificationFailed: verificationFailed,
          codeSent: codeSent,
          forceResendingToken: resendingToken,
          codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
      }
     }
    }
    

    The following gets printed to the console:

    I/flutter (32425): code sent: AM5PThC4JnFK7czWDoAdqSFjBdDk5oq9VwufNvWxgcOg4fEgbHE8CoYGuWMCjzTnfPbOlpcdfefouwL86dsD5fQs73CcR3NgvI2SRqHEHgM0n34yqqJma75ZCvPGMeTmwy6XDCA9-P0p
    I/flutter (32425): time out
    

    I've also tried waiting a few hours in case I'd reached Firebase's SMS limit, but that doesn't seem to be the problem. Any ideas?

  • Marco Fregoso
    Marco Fregoso about 5 years
    Thanks this has been really helpful, I tried this and it successfully verifies the phone number along with the verification code. However I'd still like to test Firebase sending me a real SMS. I removed the number from the "User" tab on the "Authentication" subsection as you suggested and got the following message. "This app is not authorized to use Firebase Authentication. Please verifythat the correct package name and SHA-1 are configured in the Firebase Console." I've checked and the SHA-1 signature is correctly configured, any ideas?
  • g2server
    g2server almost 5 years
    @MarcoFregoso try going to console.cloud.google.com and checking if Identity Toolkit API is ticked (if the key was restricted).
  • Oliver D
    Oliver D over 4 years
    Hey @Vineet , I just do that but I can't receive anything!! - in a Testing mode "Debugging" you know we write a test verify code and it's work well but after I release my app for Stores I can't get any SMS So should I do something maybe I forget?
  • Shahrear Bin Amin
    Shahrear Bin Amin over 2 years
    @MarcoFregoso did you get the solution? I'm facing same issue!