Firebase电话验证未通过代码发送短信

时间:2019-04-02 01:13:49

标签: android firebase dart flutter firebase-authentication

我正在尝试使用Firebase为我的Android应用设置电话号码身份验证。我已经在Firebase控制台的“身份验证”标签上启用了电话身份验证,并添加了我的号码进行测试。我尝试使用多个号码,但是尽管控制台告诉我PhoneCodeSent函数已被触发并显示成功消息,但是我没有收到任何发送给他们的短信。我尝试修复了一段时间,但似乎找不到可靠的答案。我的代码有什么问题?

我的代码如下

...
...
...
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);
  }
 }
}

以下内容将打印到控制台:

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

我还尝试等待几个小时,以防达到Firebase的SMS限制,但这似乎不是问题。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

添加电话号码进行测试时,基本上就是将其列入白名单。白名单中的电话号码用于测试电话号码身份验证,而无需发送实际的SMS消息。

没有验证码SMS发送到列入白名单的电话号码,默认的OTP为123456(除非您在添加到测试控制台时已提供了该特定号码的6位验证码)。尝试使用此代码进行验证,它将进行身份验证并生成有效的验证ID。

要获取短信,请从白名单中删除您的电话号码,然后重试。

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