Flutter iOS端Firebase Cloud Messaging推送配置问题咨询
iOS端推送通知配置指南(未注册Apple开发者计划场景)
核心限制说明
未注册Apple开发者计划的情况下,仅能在iOS模拟器上测试推送通知,真机无法正常接收——因为真机需要APNs推送证书,而证书必须通过Apple开发者账号申请。
具体配置步骤
1. 模拟器环境准备
- 使用iOS 13及以上版本的模拟器(旧版本对FCM推送支持有限)
- 打开模拟器的「设置」→「通知」,找到你的应用,确保「允许通知」开关已开启
2. Firebase项目配置
- 登录Firebase控制台,进入你的项目,在「项目设置」→「云消息传递」页面,添加iOS应用(Bundle ID必须和Xcode项目中的完全一致)
- 下载生成的
GoogleService-Info.plist文件,替换项目ios/Runner目录下的同名文件 - 打开Xcode,确认
GoogleService-Info.plist已添加到Runner目标的「Copy Bundle Resources」列表中(右键Runner→Add Files to "Runner",选择该文件)
3. Xcode项目配置
- 打开
ios/Runner.xcworkspace,选中Runner目标,切换到「Signing & Capabilities」标签 - 点击「+ Capability」,搜索并添加Push Notifications能力(即使没有开发者账号,Xcode会生成临时配置,仅适用于模拟器)
- 编辑
Info.plist,添加后台模式支持:<key>UIBackgroundModes</key> <array> <string>remote-notification</string> </array>
4. 代码调整(针对你提供的代码)
你的代码仅处理了Android端的通知展示,iOS部分存在缺失,需修改以下几点:
- 初始化
flutterLocalNotificationsPlugin时,完善iOS初始化参数:await flutterLocalNotificationsPlugin.initialize( InitializationSettings( android: const AndroidInitializationSettings('@mipmap/ic_launcher'), iOS: const IOSInitializationSettings( requestAlertPermission: true, requestBadgePermission: true, requestSoundPermission: true, )), onSelectNotification: (String? payload) async {}); - 修改
onMessage监听中的通知展示逻辑,添加iOS的NotificationDetails配置,并去掉仅判断Android的条件:FirebaseMessaging.onMessage.listen((RemoteMessage message) { RemoteNotification? notification = message.notification; if (notification != null) { flutterLocalNotificationsPlugin.show( notification.hashCode, notification.title, notification.body, NotificationDetails( android: AndroidNotificationDetails( channel.id, channel.name, channelDescription: channel.description ), iOS: const IOSNotificationDetails(), ), ); } print('foreground 상황에서 메시지를 받았다.'); print('Message data: ${message.data}'); if (message.notification != null) { print('Message also contained a notification: ${message.notification!.body}'); } });
5. 测试注意事项
- 运行模拟器后,控制台会输出FCM token,复制该token到Firebase控制台的「云消息传递」→「发送测试消息」,选择「单个设备」输入token即可测试
- 模拟器仅支持前台推送,应用退到后台后无法接收推送;真机测试必须注册Apple开发者计划并配置APNs证书
- 检查控制台输出的
settings.authorizationStatus,确保权限状态为AuthorizationStatus.authorized或AuthorizationStatus.provisional,若为denied需在模拟器设置中开启应用通知权限
内容的提问来源于stack exchange,提问作者Flutter Boi
相关产品推荐
相关产品推荐

