7

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.

rya
  • 1,417
  • 1
  • 16
  • 29

5 Answers5

14

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.

Aung Ko Man
  • 502
  • 6
  • 13
5

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

await flutterLocalNotificationsPlugin.cancel(0);

dev-aentgs
  • 1,218
  • 2
  • 9
  • 17
  • thank you for the reply but that is for canceling the notification with id value of zero – rya Jun 16 '20 at 14:21
  • ok, you need to cancel on the basis of some other parameter ? – dev-aentgs Jun 16 '20 at 14:24
  • 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 hour – rya Jun 17 '20 at 10:56
  • Are the notifications not stopping even after executing `await flutterLocalNotificationsPlugin.cancelAll();` ? – dev-aentgs Jun 17 '20 at 11:07
  • 1
    In the [API reference docs](https://pub.dev/documentation/flutter_local_notifications/latest/flutter_local_notifications/AndroidFlutterLocalNotificationsPlugin-class.html) there is a [deleteNotificationChannel method](https://pub.dev/documentation/flutter_local_notifications/latest/flutter_local_notifications/AndroidFlutterLocalNotificationsPlugin/deleteNotificationChannel.html) – dev-aentgs Jun 17 '20 at 11:14
  • @rya were you able to find a solution? – dev-aentgs Jun 24 '20 at 12:44
  • @dev-aentgs The `deleteNotificaitonChannel` method is used to remove a [notification channel](https://developer.android.com/training/notify-user/channels). The `cancel` or `cancelAll` methods should remove any scheduled and/or current notifications – Joshlucpoll Apr 06 '21 at 16:28
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.

rule_it_subir
  • 650
  • 10
  • 12
1
await flutterLocalNotificationsPlugin.cancelAll(); //cancel future note
await flutterLocalNotificationsPlugin.pendingNotificationRequests(); //restart note

And your notification will show again.

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.

Md. Al-Amin
  • 690
  • 3
  • 13