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

Flutter Android Release模式下后台通知不显示但_handler已触发

Flutter Android Release模式后台无推送通知问题解决方向

针对你遇到的Debug模式前后台通知正常、Release模式仅前台收到通知但后台handler触发的问题,可从以下几个方面排查:

1. 确认Android通知渠道配置(Android 8.0+必选)

Release模式下如果未创建通知渠道,系统会拦截后台通知。需在App初始化时创建通知渠道:

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void _createNotificationChannel() {
  const AndroidNotificationChannel channel = AndroidNotificationChannel(
    'high_importance_channel', // 渠道ID
    'High Importance Notifications', // 渠道名称
    description: 'This channel is used for important notifications.', // 渠道描述
    importance: Importance.max,
  );

  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
}

在main函数中调用该方法,确保初始化时执行。

2. 检查Firebase消息类型

Firebase推送分为两种类型:

  • 通知消息:包含notification字段,后台状态下系统会自动显示通知
  • 数据消息:仅包含data字段,后台状态下不会自动弹出通知,需在_firebaseMessagingBackgroundHandler中手动构建通知

确认你发送的是通知消息,而非纯数据消息。如果是数据消息,需按第5点手动处理。

3. 添加ProGuard/R8混淆规则

Release模式的混淆可能破坏Firebase通知逻辑,在android/app/proguard-rules.pro中添加:

-keep class com.google.firebase.** { *; }
-keep class io.flutter.plugins.firebase.messaging.** { *; }
-keep class com.google.android.gms.** { *; }

4. 验证AndroidManifest.xml配置

确保Manifest中包含必要的服务和权限:

  • 添加POST_NOTIFICATIONS权限(Android 13+):
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
  • 确认Firebase Messaging服务配置:
<service
    android:name="io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
<service
    android:name="io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundService"
    android:exported="false"
    android:permission="android.permission.BIND_JOB_SERVICE">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

5. 手动在后台handler中显示通知

如果发送的是纯数据消息,需借助flutter_local_notifications包手动构建通知:

  1. 添加依赖到pubspec.yaml:
flutter_local_notifications: ^16.1.0
  1. 修改后台handler:
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print('Handling a background message: ${message.messageId}');

  final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  const AndroidInitializationSettings initSettingsAndroid = AndroidInitializationSettings('@mipmap/ic_launcher');
  final InitializationSettings initSettings = InitializationSettings(android: initSettingsAndroid);
  await flutterLocalNotificationsPlugin.initialize(initSettings);

  const AndroidNotificationDetails androidDetails = AndroidNotificationDetails(
    'high_importance_channel',
    'High Importance Notifications',
    channelDescription: 'Channel for background notifications',
    importance: Importance.max,
    priority: Priority.high,
    enableVibration: true,
  );
  const NotificationDetails notificationDetails = NotificationDetails(android: androidDetails);

  await flutterLocalNotificationsPlugin.show(
    DateTime.now().millisecondsSinceEpoch ~/ 1000,
    message.data['title'] ?? 'New Notification',
    message.data['body'] ?? 'You have a new message',
    notificationDetails,
  );
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 11:01:09