我正在尝试使用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被发送到白名单电话号码和默认OTP 123456(除非您在添加到测试控制台时提供了该特定号码的6位验证码)。尝试使用此代码进行验证,它将进行身份验证并生成有效的验证ID。
要获取短信,请从白名单中删除您的号码,然后重试。
参考:https://firebase.google.com/docs/auth/android/phone-auth#test-with-whitelisted-phone-numbers