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

如何通过flutter_local_notifications插件获取所有已调度的本地通知?

关于flutter_local_notifications获取已调度通知列表的问题

flutter_local_notifications没有提供直接获取已调度通知列表的官方API,你需要自行持久化这些通知的信息。

原因在于这个库只是对Android系统通知API的封装,而Android系统本身并没有向第三方APP开放便捷获取已调度本地通知列表的能力(非公开系统API不仅不稳定,还可能违反应用商店规范),因此该库也没有实现这个功能。

具体实现建议

  • 每次调用zonedSchedule创建通知时,将通知的关键信息(唯一id、标题、内容、调度时间、频道配置等)存入本地存储,比如shared_preferences、hive或SQLite。
  • 取消通知时,除了调用flutterLocalNotificationsPlugin.cancel(notificationId),还要同步删除本地存储中对应的通知条目。
  • 更新已调度通知时,先修改本地存储的对应数据,再调用zonedSchedule并用同一个id覆盖原通知。

比如你用循环批量创建通知时,要确保每个通知的id唯一,同时把每个通知的信息都写入本地存储:

// 示例:循环创建通知并持久化
List<Map<String, dynamic>> scheduledNotifications = [];
for (int i = 0; i < 5; i++) {
  final notificationId = i;
  final scheduleTime = tz.TZDateTime.now(tz.local).add(Duration(minutes: 10 + i));
  // 创建通知
  await flutterLocalNotificationsPlugin.zonedSchedule(
    notificationId,
    '调度标题$i',
    '调度内容$i',
    scheduleTime,
    const NotificationDetails(
        android: AndroidNotificationDetails(
            'your channel id', 'your channel name',
            channelDescription: 'your channel description')),
    androidAllowWhileIdle: true,
    uiLocalNotificationDateInterpretation:
        UILocalNotificationDateInterpretation.absoluteTime);
  // 存入本地存储(这里用shared_preferences示例,实际可根据需求选存储方式)
  scheduledNotifications.add({
    'id': notificationId,
    'title': '调度标题$i',
    'body': '调度内容$i',
    'scheduleTime': scheduleTime.toIso8601String(),
    'channelId': 'your channel id'
  });
}
// 将列表存入shared_preferences
final prefs = await SharedPreferences.getInstance();
await prefs.setString('scheduled_notifications', jsonEncode(scheduledNotifications));

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 14:22:40