android - 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 限制,但这似乎不是问题。有任何想法吗?
解决方案
当您添加一个电话号码进行测试时,您基本上是将其列入白名单。列入白名单的电话号码用于测试电话号码身份验证,而无需发送实际的 SMS 消息。
不会向列入白名单的电话号码发送验证码 SMS,默认 OTP 为 123456(除非您在添加到测试控制台时为该特定号码提供了 6 位数验证码)。尝试使用此代码进行验证,它将进行身份验证并生成有效的验证 ID。
要获取短信,请将您的号码从白名单中删除,然后重试。
参考:https ://firebase.google.com/docs/auth/android/phone-auth#test-with-whitelisted-phone-numbers
推荐阅读
- python - 知道另一列中的元素值在同一行中获取数据(两个列名都知道)
- python - IndexError:用于绘制图形的python中的元组索引超出范围
- angular - Angular:如何自定义 FormArray 验证以检查重复项
- javascript - 如何遍历 JavaScript 中的分组对象
- algorithm - 有没有办法在没有总进程数的情况下计算进度?
- c# - 无法在 ASP.NET MVC 中触发事件
- javascript - 跨管理员和网络共享 Firestore 收集路径
- javascript - useRef() 而不是仅仅在模块范围内声明一个变量有什么好处?
- bash - 如何在 bash 中将 bash 命令作为函数参数传递?
- python - 如何在检测标签时保留 cv2.waitKey 值