Flutter每日定时通知:如何每次触发时动态修改标题与内容
解决方案:无需额外闹钟插件,用flutter_awesome_notifications实现动态内容每日通知
不需要依赖额外闹钟插件,flutter_awesome_notifications 完全可以实现每次触发时动态更新通知标题和内容。你当前的问题根源是直接创建了固定内容的系统级重复定时通知,导致内容无法动态变更。正确的做法是放弃系统重复机制,改用「单次定时通知 + 触发后自动调度下一次」的模式,每次调度时传入最新的温度数据。
具体修改步骤
1. 重构定时通知创建逻辑
移除NotificationCalendar中的repeats: true,改为创建单次定时通知,避免系统重复发送固定内容:
static Future<void> showNotification({ required final String title, required final String body, final String? summary, final Map<String, String>? payload, final ActionType actionType = ActionType.Default, final NotificationLayout notificationLayout = NotificationLayout.Default, final NotificationCategory? category, final String? bigPicture, final List<NotificationActionButton>? actionButtons, final bool scheduled = false, }) async { await AwesomeNotifications().createNotification( content: NotificationContent( id: DateTime.now().millisecondsSinceEpoch % 10000, // 用时间戳生成唯一ID,避免覆盖旧通知 channelKey: 'high_importance_channel', title: title, body: body, actionType: actionType, summary: summary, category: category, payload: payload, bigPicture: 'asset://assets/bigpicture.png', largeIcon: 'asset://assets/logonotify.png', notificationLayout: NotificationLayout.BigPicture, ), actionButtons: actionButtons, schedule: scheduled ? NotificationCalendar( hour: 3, minute: 30, allowWhileIdle: true, repeats: false, // 关闭系统自动重复,改为手动调度 timeZone: await AwesomeNotifications().getLocalTimeZoneIdentifier(), preciseAlarm: true) : null, ); }
2. 实现触发后自动重新调度
在通知显示的回调方法中,获取最新温度数据并调度第二天的通知:
static Future<void> onNotificationDisplayedMethod( ReceivedNotification receivedNotification) async { debugPrint('onNotificationDisplayedMethod'); // 替换为你实际获取当日温度和日期的逻辑 final dateToday = DateFormat('yyyy-MM-dd').format(DateTime.now()); final derivedTemperature = await fetchLatestTemperature(); // 你的温度获取接口/逻辑 // 调度次日同一时间的通知 await NotificationService.showNotification( title: 'Temperature $dateToday', body: 'Temperature: $derivedTemperature', scheduled: true, ); }
3. App启动时初始化第一次调度
在App启动时检查是否已有待触发的定时通知,若无则立即调度第一次:
// 在Main.dart的初始化代码中 void main() async { WidgetsFlutterBinding.ensureInitialized(); await NotificationService.initializeNotification(); await _setupInitialNotification(); runApp(const MyApp()); } Future<void> _setupInitialNotification() async { final scheduledNotifications = await AwesomeNotifications().listScheduledNotifications(); if (scheduledNotifications.isEmpty) { final dateToday = DateFormat('yyyy-MM-dd').format(DateTime.now()); final derivedTemperature = await fetchLatestTemperature(); await NotificationService.showNotification( title: 'Temperature $dateToday', body: 'Temperature: $derivedTemperature', scheduled: true, ); } }
关键注意事项
- 后台权限配置:Android需申请
POST_NOTIFICATIONS权限,开启后台任务权限;iOS需开启Background App Refresh,确保App在后台能执行调度逻辑。 - 温度数据兼容性:确保
fetchLatestTemperature()方法能在后台环境正常运行,避免依赖UI上下文。 - 通知ID唯一性:使用动态ID避免新通知覆盖历史通知,影响用户查看记录。
如果使用flutter_local_notifications,逻辑完全一致:通过zonedSchedule创建单次定时通知,在通知触发回调中重新调度下一次并传入最新内容。
内容的提问来源于stack exchange,提问作者SpringPost
相关产品推荐
相关产品推荐

