我正在尝试使用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的短信限制,但这似乎不是问题所在。有什么想法吗?
发布于 2019-04-02 14:16:06
当您添加用于测试的电话号码时,基本上就是将其列入白名单。白名单电话号码用于测试电话号码的身份验证,而无需发送实际的短信。
不会向白名单电话号码发送验证码短信,默认OTP为123456 (除非您在添加到测试控制台时提供了该特定号码的6位验证码)。尝试使用此代码进行验证,它将进行身份验证并生成有效的验证ID。
要获取短信,请将您的号码从白名单中删除,然后重试。
参考:https://firebase.google.com/docs/auth/android/phone-auth#test-with-whitelisted-phone-numbers
https://stackoverflow.com/questions/55465594
复制相似问题