Flutter Firebase 电话身份验证未发送短信代码。直接调用 PhoneCodeAutoRetrievalTimeout 方法。这是我的代码
Posted
技术标签:
【中文标题】Flutter Firebase 电话身份验证未发送短信代码。直接调用 PhoneCodeAutoRetrievalTimeout 方法。这是我的代码【英文标题】:Flutter Firebase Phone Authentication is not sending sms code. PhoneCodeAutoRetrievalTimeout method is called directly. Here is my code 【发布时间】:2021-01-08 16:35:00 【问题描述】:我刚刚复制粘贴了示例代码以开始使用 Firebase 电话身份验证。但是 firebase 不会向真实号码和测试号码发送 SMS 代码。我对两者都进行了测试。你能帮忙找出问题吗?
这是 Flutter Doctor 的输出
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.20.4, on Microsoft Windows [Version 10.0.19041.508], locale en-IN)
[√] android toolchain - develop for Android devices (Android SDK version 29.0.2)
[√] Android Studio (version 4.0)
[√] VS Code (version 1.49.1)
[√] Connected device (1 available)
• 未发现任何问题!
import 'package:firebase_auth/firebase_auth.dart';
class AuthService
String _phoneNumber;
String _verificationId;
String _smsCode;
String _message;
final FirebaseAuth _auth = FirebaseAuth.instance;
String get phoneNumber1 => _phoneNumber;
String get verificationId => _verificationId;
String get smsCode => _smsCode;
String get message => _message;
Future<void> verifyPhoneNumber(String phoneNumber) async
print(phoneNumber);
PhoneVerificationCompleted verificationCompleted =
(PhoneAuthCredential phoneAuthCredential) async
print('User Auto Logged In');
// UserCredential _userCredential =
// await _auth.signInWithCredential(phoneAuthCredential);
;
PhoneVerificationFailed verificationFailed =
(FirebaseAuthException authException) async
print('Auth Exception Occured');
// throw Exception(authException.message);
// _message =
// 'Phone number verification failed. Code: $authException.code. Message: $authException.message';
;
PhoneCodeSent codeSent =
(String verificationId, [int forceResendingToken]) async
print('SMS Sent');
_verificationId = verificationId;
;
PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
(String verificationId) async
print('SMS Auto Retrieval Timeout');
_verificationId = verificationId;
;
await _auth.verifyPhoneNumber(
phoneNumber: phoneNumber,
timeout: const Duration(seconds: 5),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
// Example code of how to sign in with phone.
void signInWithPhoneNumber(String smsCode) async
final AuthCredential credential = PhoneAuthProvider.credential(
verificationId: _verificationId,
smsCode: smsCode,
);
final User user = (await _auth.signInWithCredential(credential)).user;
输出:
I/BiChannelGoogleApi( 2258): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@57888fb
Reloaded 4 of 704 libraries in 1,013ms.
I/flutter ( 2258): SMS Auto Retrieval Timeout
请帮我解决这个问题。
【问题讨论】:
你在模拟器上测试它吗? 不,它是真实设备。我已经实现了这个。但是这次没有发送短信,控制台也没有显示消息消息。 【参考方案1】:问题出在火力基地本身。那天它没有发送任何短信。现在相同的代码运行良好。
【讨论】:
相同的火力基地不发送任何短信。代码发送回调被调用,但 firebase 没有发送任何短信。以上是关于Flutter Firebase 电话身份验证未发送短信代码。直接调用 PhoneCodeAutoRetrievalTimeout 方法。这是我的代码的主要内容,如果未能解决你的问题,请参考以下文章
Flutter + Firebase Auth:有啥方法可以在 Web 上使用 Firebase 电话身份验证重新发送短信验证码?
使用 BLoC 进行 Flutter Firebase 电话身份验证
Firebase 电话身份验证 (Flutter) 在某些 iOS 设备中不起作用