i am developing an app in which all notifications are schedule in advance. Each notification is set to a different time of the day and should replace the previous notification.
I.e 9.00 AM, 1.00 PM, 5.00 PM, Etc
I am using the package https://pub.dev/packages/flutter_local_notifications and the code below:
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);
Although the problem i am having is i want each new notification to replace the previous notification, if the user hasn't seen that notification yet. So the user isn't spammed with 20 new notifications when they return to their phone. If i use the same ID for each notification then it replaces the previous notification but since the notifications are all scheduled at the same time (i.e when the user first opens the app) rather than creating multiple scheduled notifications that replace their respective predecessor after being displayed. It just removes all the scheduled notifications and only schedules that last. If i use a unique id for each notification then it creates a new notification without removing the previous. I also cant use the notifications.cancel(id);
function as the app might not be open when the user it notified.
Any ideas, Thanks.