How to unschedule previously scheduled local notifications after some time?
Asked Answered
P

5

9

I am using flutter_local_notifications library to schedule local notifications every 1 hour. It is working as expected but now, I need a way to start/ stop the notification schedule (say, at push of a button).

I could not find anything regarding canceling scheduled notification requests in the documentation.

Pampuch answered 16/6, 2020 at 10:55 Comment(0)
R
17

Cancelling/deleting a notification

// cancel the notification with id value of zero
await flutterLocalNotificationsPlugin.cancel(0);
// 0 is your notification id

Cancelling/deleting all notifications

await flutterLocalNotificationsPlugin.cancelAll();

And yes, this can cancel current and future notification. Just make sure correct notification id.

Ripplet answered 5/11, 2020 at 3:10 Comment(0)
E
5

In the docs it is given in Cancelling/deleting a notification

await flutterLocalNotificationsPlugin.cancel(0);

Ecker answered 16/6, 2020 at 13:30 Comment(7)
thank you for the reply but that is for canceling the notification with id value of zeroPampuch
ok, you need to cancel on the basis of some other parameter ?Ecker
no all .cancel() does is remove the notification from the notification tray. I want to be able to stop future notifications that will pop up every hourPampuch
Are the notifications not stopping even after executing await flutterLocalNotificationsPlugin.cancelAll(); ?Ecker
In the API reference docs there is a deleteNotificationChannel methodEcker
@Pampuch were you able to find a solution?Ecker
@Ecker The deleteNotificaitonChannel method is used to remove a notification channel. The cancel or cancelAll methods should remove any scheduled and/or current notificationsBloater
S
5

I would do something like this:

final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();
// Below will give you any unpresented/scheduled notifications
final List<PendingNotificationRequest> pendingNotificationRequests =
    await _flutterLocalNotificationsPlugin.pendingNotificationRequests();
for (var _pendingRequest in pendingNotificationRequests) {
  _flutterLocalNotificationsPlugin.cancel(_pendingRequest.id);
} 

Here _pendingRequest.id will give you the required notification id to cancel the specific notification request. This is tested in Android. Will update the status on iOS soon.

Squash answered 21/4, 2022 at 7:9 Comment(1)
This is perfect if you don't want to delete the existing notification if it has already happened.Inofficious
M
2
await flutterLocalNotificationsPlugin.cancelAll(); //cancel future note
await flutterLocalNotificationsPlugin.pendingNotificationRequests(); //restart note

And your notification will show again.

Managua answered 30/8, 2022 at 15:37 Comment(0)
W
1

If you want to cancel all notifications, then you have to do that using the following:

await flutterLocalNotificationsPlugin.cancelAll();

Also, if you want to cancel a specific notification, you can do this by using a specific ID of your notification:

await flutterLocalNotificationsPlugin.cancel(0);  // 0 is your notification id

Where this notification ID came from ??

While you are declaring your notification to show on your screen you have to add some data like given below:

await _flutterLocalNotificationsPlugin.show(
        0, // this is the notification id
        message.notification.title,
        message.notification.body,
        notificationDetails,
        payload: jsonEncode(message.data),
      );

Here I am using Flutter Local Notification Plugin to show notifications!!!

Hope this will solve your problem.

Wenzel answered 15/12, 2022 at 3:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.