How to unschedule previously scheduled local notifications after some time?

Viewed 4153

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.

4 Answers

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.

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

await flutterLocalNotificationsPlugin.cancel(0);

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.

await flutterLocalNotificationsPlugin.cancelAll(); //cancel future note
await flutterLocalNotificationsPlugin.pendingNotificationRequests(); //restart note

And your notification will show again.

Related