'How to unschedule previously scheduled local notifications after some time?
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.
Solution 1:[1]
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.
Solution 2:[2]
In the docs it is given in Cancelling/deleting a notification
await flutterLocalNotificationsPlugin.cancel(0);
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ko Man |
Solution 2 | dev-aentgs |
Solution 3 | rule_it_subir |