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

适配Android S+(API31+)时PendingIntent需指定FLAG_IMMUTABLE/MUTABLE问题求助

解决Targeting S+(API 31+)PendingIntent flag缺失问题

针对你遇到的Android 12+要求PendingIntent必须指定FLAG_IMMUTABLE或FLAG_MUTABLE的问题,按以下步骤解决:

1. 升级flutter_local_notifications库

首先确保你使用的是最新版本的flutter_local_notifications,新版本已默认处理该flag。在pubspec.yaml中更新依赖:

flutter_local_notifications: ^最新版本号

执行flutter pub get完成依赖更新。

2. 手动配置PendingIntent Flags(升级后仍报错时)

若升级库后问题依旧,需在创建AndroidNotificationDetails时显式指定pendingIntentFlags。修改你的_notificationDetails方法:

Future<NotificationDetails> _notificationDetails() async {
  const AndroidNotificationDetails androidDetails = AndroidNotificationDetails(
    'water_reminder_channel', // 替换为你的渠道ID
    'Water Reminder', // 替换为你的渠道名称
    channelDescription: 'Reminds you to drink water regularly', // 替换为你的渠道描述
    importance: Importance.max,
    priority: Priority.high,
    // 关键:添加flag配置
    pendingIntentFlags: PendingIntentFlags.immutable,
  );
  const IOSNotificationDetails iosDetails = IOSNotificationDetails();
  return const NotificationDetails(android: androidDetails, iOS: iosDetails);
}

如果你的通知需要后续修改PendingIntent内容,可将flag改为PendingIntentFlags.mutable,否则使用immutable即可。

3. 补充Android 13+通知权限

若targetSdk为33及以上,需在android/app/src/main/AndroidManifest.xml中添加通知权限:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

修改完成后重新运行项目,调用showLocalNotification即可正常触发通知,不会再出现flag缺失的报错。

内容的提问来源于stack exchange,提问作者Ajeet Kumar Sah

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 06:30:09