首页 > 解决方案 > Firebase 电话身份验证未发送带有代码的短信

问题描述

我正在尝试使用 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 限制,但这似乎不是问题。有任何想法吗?

标签: androidfirebasedartflutterfirebase-authentication

解决方案


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

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

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

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


推荐阅读