我正在开发一个应用程序,其中的所有通知都是预先安排的。每个通知均设置为一天中的不同时间,并应替换之前的通知。
即9.00 AM,1.00 PM,5.00 PM等
我正在使用软件包https://pub.dev/packages/flutter_local_notifications和以下代码:
for (var interval in [1, 3, 4, 7, 7.5, 8]) {
var time = (appStart).add(Duration(hours: interval));
scheludeNotification(notifications,
title: "Title",
body: "Body"
time: time,
id: 0, // OR unique id
payload: '');
}
Future scheludeNotification(
FlutterLocalNotificationsPlugin notifications, {
@required String title,
@required String body,
@required DateTime time,
@required String payload,
int id = 0,
}) =>
_scheduleNotification(notifications,
title: title,
body: body,
id: id,
time: time,
type: _ongoing,
payload: payload);
Future _scheduleNotification(
FlutterLocalNotificationsPlugin notifications, {
@required String title,
@required String body,
@required DateTime time,
@required NotificationDetails type,
@required String payload,
int id = 0,
}) =>
notifications.schedule(id, title, body, time, type, payload: payload);
尽管我遇到的问题是,如果用户尚未看到该通知,我希望每个新通知都可以替换以前的通知。因此,当用户返回手机时,不会收到20条新通知。如果我为每个通知使用相同的ID,则它将替换先前的通知,但是由于所有通知都是在同一时间安排的(即,当用户首次打开应用程序时),而不是创建多个安排的通知,以在显示后替换各自的前任通知。它只删除所有计划的通知,仅保留最后的计划。如果我为每个通知使用唯一的ID,则它将创建一个新通知而不删除以前的通知。我也无法使用notifications.cancel(id);
函数,因为当用户通知应用程序时,该应用程序可能无法打开。
任何想法,谢谢。