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

Flutter本地通知前台不显示问题求助

Flutter本地通知前台不显示,后台正常的解决方案

核心问题

flutter_local_notifications插件默认逻辑是:应用后台时自动展示通知栏,但前台时不会主动弹出通知,得针对Android和iOS分别配置前台展示规则。

具体修复步骤

1. 补全Android端配置

  • 给AndroidNotificationDetails加priority: Priority.high,保证前台通知的优先级足够触发系统展示
  • 初始化时主动创建通知渠道,避免渠道未注册导致通知“隐形”
  • 你已经把渠道importance设为Importance.max了,这部分没问题不用改

修改NotificationApi的init方法,增加渠道创建逻辑:

static Future init({bool initScheduled = false}) async {
  const android = AndroidInitializationSettings('@mipmap/ic_launcher');
  const iOS = IOSInitializationSettings(
    requestAlertPermission: true,
    requestBadgePermission: true,
    requestSoundPermission: true,
  );
  const settings = InitializationSettings(android: android, iOS: iOS);

  // 主动创建Android通知渠道
  final androidPlugin = _notifications.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
  await androidPlugin?.createNotificationChannel(const AndroidNotificationChannel(
    'channel id 8',
    'channel name',
    description: 'channel description',
    importance: Importance.max,
  ));

  final details = await _notifications.getNotificationAppLaunchDetails();
  if (details != null && details.didNotificationLaunchApp) {
    onNotifications.add(details.payload);
  }

  await _notifications.initialize(
    settings,
    onSelectNotification: (payload) async {
      onNotifications.add(payload);
    },
    // 可选:前台收到通知时的自定义回调,比如弹个弹窗
    onDidReceiveLocalNotification: (id, title, body, payload) async {
      // 这里可以加自定义弹窗逻辑,不写也不影响系统通知栏展示
    },
  );

  if (initScheduled) {
    tz.initializeTimeZones();
    final locationName = await FlutterNativeTimezone.getLocalTimezone();
    tz.setLocalLocation(tz.getLocation(locationName));
  }
}

更新_notificationDetails里的Android配置:

static Future _notificationDetails() async {
  const sound = 'notification_sound.wav';
  return NotificationDetails(
    android: AndroidNotificationDetails(
      'channel id 8',
      'channel name',
      channelDescription: 'channel description',
      importance: Importance.max,
      priority: Priority.high, // 新增这一行
      sound: RawResourceAndroidNotificationSound(sound.split('.').first),
      enableVibration: false,
      channelShowBadge: true,
    ),
    iOS: const IOSNotificationDetails(
      presentAlert: true, // 强制前台展示通知弹窗
      presentSound: false,
      sound: sound,
      presentBadge: true,
    ),
  );
}

2. 完善iOS端配置

  • 初始化时明确请求alert、badge、sound权限
  • 通知详情里开presentAlert: true,确保前台能弹出通知

上面的代码已经包含这些配置,另外要在Info.plist里加权限描述(不然iOS会拒权限):

<key>NSAlertUsageDescription</key>
<string>需要显示通知提醒</string>
<key>NSBadgeUsageDescription</key>
<string>需要更新应用角标</string>
<key>NSSoundUsageDescription</key>
<string>需要播放通知声音</string>

3. Android 13+权限验证

Android 13及以上版本单独要求POST_NOTIFICATIONS权限,要是之前没处理,得加个权限请求:

import 'package:permission_handler/permission_handler.dart';

// 在init方法里加这段(记得判断平台)
if (Platform.isAndroid) {
  final status = await Permission.notification.request();
  if (!status.isGranted) {
    // 可以提示用户去设置开权限
  }
}

测试

改完重新跑App,触发定时通知后,前台状态下应该能正常看到通知栏弹出了。

内容的提问来源于stack exchange,提问作者Laurent Thomas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 15:15:46