Flutter iOS端Firebase推送获取Token超时错误求助
Flutter iOS Firebase推送通知获取Token超时问题
问题详情
Flutter应用集成Firebase推送通知后,Android端功能正常,但真实iOS设备获取Token时触发请求超时错误,已严格按照官方文档完成配置。
环境信息
- Xcode 15.2
- Flutter 3.19.5
错误日志
FLTFirebaseMessaging: An error occurred while calling method Messaging#getToken, errorOrNil => { NSErrorFailingURLKey = "https://device-provisioning.googleapis.com/checkin"; NSErrorFailingURLStringKey = "https://device-provisioning.googleapis.com/checkin"; NSLocalizedDescription = "The request timed out."; "_NSURLErrorFailingURLSessionTaskErrorKey" = "LocalDataTask <9BEF5303-AEAA-4005-9A7D-D8307A35D3D5>.<1>"; "_NSURLErrorRelatedURLSessionTaskErrorKey" = ( "LocalDataTask <9BEF5303-AEAA-4005-9A7D-D8307A35D3D5>.<1>" ); "_kCFStreamErrorCodeKey" = "-2103"; "_kCFStreamErrorDomainKey" = 4; }
初始化代码
Future<void> initFirebase() async { String? fcmToken; await Firebase.initializeApp( options: FirebaseOptions( apiKey: Platform.isAndroid ? "AIzaSyBuRrD9IUg721orGNJzUPw_6jBqzW0qbgcw" : "AIzaSyDpy26KbxDkillvYaBlX_lqvmXC1XOQSpA", appId: Platform.isAndroid ? "1:199061089561:android:bc4deb3u87dd11a1af0d90" : '1:199061089961:ios:09e1ew1avp141a86af0d90', messagingSenderId: "199061680561", iosBundleId: "com.flutter.truck", storageBucket: "truck-19121.appspot.com", projectId: "truck-19121")); if (Platform.isAndroid) { fcmToken = await FirebaseMessaging.instance.getToken(); } else if (Platform.isIOS) { fcmToken = await FirebaseMessaging.instance.getAPNSToken(); if (fcmToken != null) { await Future<void>.delayed( const Duration( seconds: 5, ), ); fcmToken = await FirebaseMessaging.instance.getAPNSToken(); } } printColored("FCM USER TOKEN: $fcmToken", PrintedColors.red); // FOREGROUND MESSAGE HANDLING. FirebaseMessaging.onMessage.listen((RemoteMessage message) { debugPrint( "NOTIFICATION FOREGROUND MODE: ${message.notification!.title} in foreground"); debugPrint("NOTIFICATION DATA: ${message.data} in terminated"); LocalNotificationService.localNotificationService .showRemoteNotification(message); }); // BACkGROUND MESSAGE HANDLING FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); // FROM TERMINATED MODE handleMessage(RemoteMessage message) { debugPrint( "NOTIFICATION FROM TERMINATED MODE: ${message.notification!.title} in terminated"); debugPrint("NOTIFICATION DATA: ${message.data} in terminated"); LocalNotificationService.localNotificationService .showRemoteNotification(message); } RemoteMessage? remoteMessage = await FirebaseMessaging.instance.getInitialMessage(); if (remoteMessage != null) { handleMessage(remoteMessage); } FirebaseMessaging.onMessageOpenedApp.listen(handleMessage); } Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { await Firebase.initializeApp(); LocalNotificationService.localNotificationService .showRemoteNotification(message); debugPrint( "NOTIFICATION BACKGROUND MODE: ${message.notification!.title} in background"); debugPrint("NOTIFICATION DATA: ${message.data} in terminated"); }
解决思路与方案
1. 验证网络与域名可达性
错误指向device-provisioning.googleapis.com请求超时,先确认设备网络状态:
- 用Safari访问
https://device-provisioning.googleapis.com/checkin,只要不超时(返回400属于正常响应),说明域名可访问 - 切换WiFi/蜂窝数据测试,排除网络环境问题
2. 修正iOS Token获取逻辑
当前iOS分支仅获取APNS Token,但Firebase推送需要的是FCM Token,需调整代码:
else if (Platform.isIOS) { // 先申请推送权限 NotificationSettings settings = await FirebaseMessaging.instance.requestPermission( alert: true, badge: true, sound: true, ); if (settings.authorizationStatus == AuthorizationStatus.authorized) { fcmToken = await FirebaseMessaging.instance.getToken(); } }
3. 检查Xcode配置细节
- 确认
GoogleService-Info.plist已添加到Runner.xcworkspace,且Target Membership勾选正确 - 打开Capabilities,确保
Push Notifications已启用,Background Modes勾选Remote notifications - 核对Bundle ID与Firebase控制台注册的iOS应用ID完全一致
- 确认Provisioning Profile包含APNs推送权限(需使用带推送功能的开发/生产证书)
4. 优化初始化时机与重试机制
避免应用启动时立即请求Token,可推迟到用户登录后或应用完全初始化完成;添加超时重试:
// 封装带重试的Token获取方法 Future<String?> getFCMTokenWithRetry({int retries = 3}) async { for (int i = 0; i < retries; i++) { try { return await FirebaseMessaging.instance.getToken().timeout(const Duration(seconds: 10)); } catch (e) { if (i == retries - 1) rethrow; await Future.delayed(const Duration(seconds: 2)); } } return null; }
5. 升级依赖版本
检查pubspec.yaml中firebase_messaging、firebase_core的版本,升级到适配Flutter 3.19.5的最新稳定版,规避版本兼容问题。
内容的提问来源于stack exchange,提问作者Sardor Sirojov
相关产品推荐
相关产品推荐

