You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 19:01:29