我正在使用 flutter_local_notifications 插件并尝试让 zonedSchedule 方法在我指定的特定时间用于动态重复每日通知。
该插件的配置是正确的,因为我能够显示通知并使用他们文档中的示例重复通知(从现在起 5 秒):
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'scheduled title',
'scheduled body',
tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
const NotificationDetails(
android: AndroidNotificationDetails(
'your channel id', 'your channel name',
channelDescription: 'your channel description')),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
我想做的是安排一个通知在每天的特定时间重复。我正在尝试利用这个逻辑:
tz.TZDateTime _nextInstanceOf(int time) {
// print('wl here!!');
// print('time: ');
// print(time);
final now = tz.TZDateTime.now(tz.local);
var scheduledDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, time, 31, 0);
// print("scheduledDate: ");
// print(scheduledDate);
if (scheduledDate.isBefore(now)) {
// print('wl update!!');
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}
和
Future<void> _scheduleDailyNotification(int time, String msg) async {
print('wl schedule notification!!');
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'Reminder:',
msg,
_nextInstanceOf(time),
const NotificationDetails(
android: AndroidNotificationDetails(
'daily notification channel id',
'daily notification channel name',
'daily notification description'),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
payload: 'wltry'
);
}
任何帮助表示赞赏。