Flutter iOS端FCM onSelectNotification点击回调不生效如何解决
iOS前台点击flutter_local_notifications通知不触发onSelectNotification回调解决方案
- 不需要在
Info.plist中添加类似Android端CLICK_NOTIFICATION的专属配置项,iOS无对应配置,问题出在初始化参数和API适配层面,Android端原有逻辑可正常运行无需调整。 - 第一步补全iOS初始化的权限与前台展示参数,原代码中
IOSInitializationSettings()未传任何配置,iOS 10+ 前台默认不展示通知、不响应点击事件,修改初始化代码如下:
var androiInit = AndroidInitializationSettings('@mipmap/ic_launcher');//for logo var iosInit = IOSInitializationSettings( requestAlertPermission: true, requestBadgePermission: true, requestSoundPermission: true, defaultPresentAlert: true, defaultPresentBadge: true, defaultPresentSound: true, ); var initSetting=InitializationSettings(android: androiInit,iOS: iosInit); var fltNotification = FlutterLocalNotificationsPlugin(); // 兼容新旧版本回调 fltNotification.initialize( initSetting, onSelectNotification: onSelectNotification, // 高版本flutter_local_notifications必须加这个回调才能响应点击 onDidReceiveNotificationResponse: (response) { print("test"); } );
- 第二步检查发送通知时的iOS参数配置,发送通知时必须显式开启前台展示选项,否则通知不会在前台弹出,自然无法触发点击:
const iOSDetail = IOSNotificationDetails( presentAlert: true, presentBadge: true, presentSound: true, ); const notifyDetail = NotificationDetails(iOS: iOSDetail); // 调用show方法发送通知时传入上述配置 await fltNotification.show(0, '标题', '内容', notifyDetail, payload: 'test');
- 第三步注意生效规则:修改上述配置后必须完全停止App、重新全量编译运行,热重载/热重启不会生效,首次启动时弹出通知权限申请弹窗需点击允许。
- 原有回调函数无需额外修改:
Future<void> onSelectNotification(String? payload) async { print("test"); }
内容的提问来源于stack exchange,提问作者Wagner Tiburcio
相关产品推荐
相关产品推荐

