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

2024年Flutter实现iOS前台推送通知解决方案

iOS前台接收Firebase推送通知的解决方案

问题场景

我正在开发一款基于Flutter、集成Firebase推送功能的应用。Android端推送运行正常,iOS端后台推送也正常,但iOS应用处于前台模式时,通知不会直接显示,仅能通过手动下拉系统通知中心查看。请问是否可以通过Flutter实现iOS前台接收推送通知?

解决方案

完全可以实现。你的代码已配置了基础的FCM权限与前台展示参数,但前台消息处理方法_fcmForegroundHandler未对iOS做通知展示逻辑,这是问题核心。

修改步骤

  1. 完善前台消息处理方法,添加iOS的通知展示逻辑:
    将_fcmForegroundHandler方法补充iOS分支的处理,和后台逻辑一致调用AwesomeNotificationsHelper弹出通知:
//handle fcm notification when app is open
static Future<void> _fcmForegroundHandler(RemoteMessage message) async {
  print("foreground message : ${message.data}");
  if (message.data.isEmpty && message.data['title'] == null) {
    return;
  }

  if (GetPlatform.isAndroid) {
    AwesomeNotificationsHelper.showNotification(
      id: 1,
      title: message.data['title'] ?? 'Tittle',
      body: message.data['body'] ?? 'Body',
      payload: message.data
          .cast(), // pass payload to the notification card so you can use it (when user click on notification)
      actionButtons: [],
      notificationLayout: NotificationLayout.Default,
    );
  }

  // 新增iOS前台通知处理逻辑
  if (GetPlatform.isIOS) {
    AwesomeNotificationsHelper.showNotification(
      id: 1,
      title: message.notification?.title ?? 'Tittle',
      body: message.notification?.body ?? 'Body',
      payload: message.data
          .cast(),
      actionButtons: [],
      notificationLayout: NotificationLayout.Default,
    );
  }

  // navigate to notification screen
  // Get.toNamed(NotificationScreen.route);
}
  1. 确认基础配置有效性:
    你已在_setupFcmNotificationSettings中设置了setForegroundNotificationPresentationOptions(alert: true, sound: true, badge: true),这一步配置正确。同时需确保iOS系统设置中,已为应用开启横幅通知、声音等权限。

补充说明

iOS系统特性决定了:应用前台时Firebase不会自动弹出系统通知,必须通过本地通知插件(你使用的awesome_notifications)手动触发展示;而后台状态下系统会自动处理推送,所以之前后台推送正常。修改后,前台收到推送时会通过插件直接弹出通知。

另外建议不要固定通知ID为1,可改用时间戳或消息唯一ID,避免新通知覆盖旧通知。

内容的提问来源于stack exchange,提问作者NITH APPLE

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 09:30:36