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

Android 13+应用前台时Firebase通知不显示于通知栏问题

问题解决:Flutter前台状态下Firebase通知无法显示在通知栏

问题分析

你的代码已能成功接收前台Firebase消息并打印日志,但通知栏不显示,核心问题通常出在flutter_local_notifications初始化不完整、Android通知渠道未创建、异步调用未等待或iOS权限/配置缺失这几个方面。

具体修复步骤

1. 完善flutter_local_notifications初始化

确保在main()中完成插件的完整初始化,覆盖Android和iOS配置:

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  
  // 初始化flutter_local_notifications
  const AndroidInitializationSettings androidInit = AndroidInitializationSettings('@mipmap/ic_launcher');
  const IOSInitializationSettings iosInit = IOSInitializationSettings(
    requestAlertPermission: true,
    requestBadgePermission: true,
    requestSoundPermission: true,
  );
  const InitializationSettings initSettings = InitializationSettings(
    android: androidInit,
    iOS: iosInit,
  );
  await flutterLocalNotificationsPlugin.initialize(initSettings);

  // 注册后台消息处理
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  
  // 创建Android通知渠道(API 26+必需)
  await createNotificationChannel();
  
  // 配置iOS前台通知显示权限
  await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
    alert: true,
    badge: true,
    sound: true,
  );

  runApp(MyApp());
}

2. 创建Android通知渠道

Android 8.0及以上系统强制要求创建通知渠道才能显示通知,添加以下函数并在初始化时调用:

Future<void> createNotificationChannel() async {
  const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'channelId', // 必须与showLocalNotification中的channelId完全一致
    'channelName',
    description: 'channelDescription',
    importance: Importance.high,
    playSound: true,
  );

  await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
}

3. 前台监听中等待通知显示完成

你的前台消息监听调用showLocalNotification时未加await,异步函数可能未执行完毕就跳过,导致通知未触发:

FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
  print("foregroundMsg:${message.notification?.title}");
  print("foregroundMsg:${message.notification?.body}");
  print("foregroundMsg1:${message.notification}");
  
  // 添加await,确保通知显示逻辑执行完成
  await showLocalNotification(
      0, message.notification?.title ?? '', message.notification?.body ?? '');

  try {
    print("foreground2 ${message.notification?.title}");
    CheckInternet().getUnreadNotificationCount();

    await NotificatioDb.instance.insertNotification(
          "${message.notification?.title}",
          "${message.notification?.body}",
          "${message.data['time']}",
          "${message.data['url']}",
          "${message.data['reloadFlag']}");
    print("foreground3 ${message.notification?.title}");
  } catch (e) {
    print("foreground4 ${message.notification?.title}");
    print("backgroundErrorlisten $e");
  }
});

4. 确认iOS通知权限

确保已请求iOS的通知权限,可在初始化阶段添加:

await FirebaseMessaging.instance.requestPermission(
  alert: true,
  badge: true,
  sound: true,
);

额外检查点

  • 确认showLocalNotification中的channelId和创建通知渠道时的ID完全一致,大小写敏感。
  • 使用真实设备测试,部分模拟器可能存在通知显示异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 23:31:08