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

如何触发FlutterLocalNotificationsPlugin的onDidReceiveBackgroundNotificationResponse回调?

解决flutter_local_notifications后台回调onDidReceiveBackgroundNotificationResponse不触发问题

针对Android设备上Flutter 3.10.6 + flutter_local_notifications ^14.1.0的配置问题,按以下步骤排查修复:

1. 完善AndroidManifest.xml配置

确保在android/app/src/main/AndroidManifest.xml中添加必要的权限和组件:

  • 添加权限(Android 13+需POST_NOTIFICATIONS):
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    
  • 注册插件的后台组件:
    <application ...>
      <!-- 本地通知相关组件 -->
      <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
      <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
          <intent-filter>
              <action android:name="android.intent.action.BOOT_COMPLETED"/>
              <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
          </intent-filter>
      </receiver>
      <service android:name="com.dexterous.flutterlocalnotifications.ForegroundService" />
      <service android:name="com.dexterous.flutterlocalnotifications.BackgroundNotificationService" />
    </application>
    

2. 正确定义后台回调函数

后台回调必须是顶级函数或静态方法,且添加@pragma('vm:entry-point')注解,避免被Flutter编译器优化移除:

// 顶级函数,放在main.dart或单独文件中
@pragma('vm:entry-point')
void handleBackgroundNotificationResponse(NotificationResponse response) {
  debugPrint('后台通知响应触发,payload: ${response.payload}');
  // 在这里执行你的后台操作逻辑
}

初始化插件时关联该回调:

final FlutterLocalNotificationsPlugin notificationsPlugin = FlutterLocalNotificationsPlugin();

Future<void> initNotifications() async {
  const AndroidInitializationSettings androidInit = AndroidInitializationSettings('@mipmap/ic_launcher');
  const InitializationSettings initSettings = InitializationSettings(android: androidInit);

  await notificationsPlugin.initialize(
    initSettings,
    onDidReceiveBackgroundNotificationResponse: handleBackgroundNotificationResponse,
    onDidReceiveNotificationResponse: (response) {
      // 前台通知点击回调
    },
  );
}

3. 确保通知频道配置正确(Android 8+)

Android 8及以上版本要求通知必须绑定有效频道,否则通知无法正常触发回调:

// 创建通知频道
const AndroidNotificationChannel channel = AndroidNotificationChannel(
  'custom_notification_channel', // 唯一频道ID
  '自定义通知频道', // 频道名称
  description: '用于接收本地推送通知', // 频道描述
  importance: Importance.max,
  priority: Priority.high,
);

// 初始化时创建频道
await notificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
    ?.createNotificationChannel(channel);

调用show方法时必须指定该频道ID:

await notificationsPlugin.show(
  0, // 通知ID
  '后台测试通知',
  '点击我触发后台回调',
  NotificationDetails(
    android: AndroidNotificationDetails(
      'custom_notification_channel', // 与创建的频道ID一致
      '自定义通知频道',
      channelDescription: '用于接收本地推送通知',
      importance: Importance.max,
      priority: Priority.high,
    ),
  ),
  payload: 'test_background_payload', // 自定义负载
);

4. 正确测试后台场景

  • 启动应用后,按Home键让应用进入后台(不要杀死进程)
  • 触发通知展示,点击通知即可触发后台回调
  • 若应用被完全杀死(从最近应用列表划掉),后台回调可能无法触发(需额外配置前台服务或唤醒机制)

5. 关闭或配置混淆规则

如果开启了ProGuard/R8混淆,需在android/app/proguard-rules.pro中添加规则保护插件类:

-keep class com.dexterous.flutterlocalnotifications.** { *; }
-keep class androidx.core.app.** { *; }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 10:43:11