Search code examples
flutterdurationflutter-local-notification

Local Notifications 2023. How to set periodically notifications?


I'm working in an app and I need to implement local notification based on user input. This means that the user will select when the app shows the notification. I found the method repeatInterval, but it only offers fixed time intervals such as

everyMinute → const RepeatInterval

hourly → const RepeatInterval

daily → const RepeatInterval

weekly → const RepeatInterval

I don't need these options, because they are fixed. I need to add a notification, and the interval on runtime.

I tried with,

NotificationDetails notificationDetails =
 NotificationDetails(android: androidNotificationDetails);
await _flutterLocalNotificationsPlugin.zonedSchedule(
  -1,
  title,
  body,
  tz.TZDateTime.from(
    DateTime.now().add(const Duration(hours:  hours)), 
    tz.local
  ),
  androidAllowWhileIdle: true,
  notificationDetails,
  uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime
);

With this method I was able to pass manually the first notification, but I need to repeat the notification X times until the user cancel the notification.

Any ideas?


Solution

  • The flutter_local_notifications plugin does not provide this functionality by itself. You have the following options.

    • You could create your own fork of the plugin and create variants of the periodically_show function. The android implementation and iOS implementation of periodically_show internally support any time interval. It's just the API design of the plugin that choses to hide this functionality.
    • You could combine flutter_local_notifications with a background worker library WorkerManager that allows your app to execute some functionality in the background. On each call you could schedule a notification.
    • You could calculate the scheduling time X times in advance and schedule your notifications in batch. Then, when the user changes their notification preferences, you can cancel them all again.

    I think such functionality would make a great contribution to the flutter_local_notifications library.