2

I am trying to send out notifications every 72 hours. I am using the flutter_local_notifications package. I know I can periodically show notifications but as far as I can see it is limited to these options:

/// The available intervals for periodically showing notifications.
enum RepeatInterval {
  /// An interval for every minute.
  everyMinute,

  /// Hourly interval.
  hourly,

  /// Daily interval.
  daily,

  /// Weekly interval.
  weekly
}

Is there any way to achieve the 72h interval? I couldn't find anything on this. Let me know if you need any more info! Any help is appreciated!

Chris
  • 1,828
  • 6
  • 40
  • 108
  • Is it fix for 72 hours or can be changed by someone into minutes or days etc? – Diwyansh Dec 22 '21 at 09:55
  • @Diwyansh not quite sure if I understand your question. 72h is fixed – Chris Dec 22 '21 at 09:56
  • Actually you have options to schedule the notification for next 72 hours whenever the first 72 hours get completed or you can show them immediately when it detects 72 hours what will you prefer? – Diwyansh Dec 22 '21 at 10:11
  • @Diwyansh the notification should be sent every 72h with at a given time – Chris Dec 22 '21 at 11:19

3 Answers3

1

you can try this :-

 fltrNotification = new FlutterLocalNotificationsPlugin();

 var scheduledTime = DateTime.now().add(Duration(hour : 72));
 fltrNotification.schedule(1, "Times Uppp", task, 
 scheduledTime, generalNotificationDetails);
Piyush Kumar
  • 460
  • 2
  • 7
  • 1
    This would result in only 1 notification, or am I wrong? I would like to repeat that notification in a72h interval – Chris Dec 23 '21 at 08:59
  • 1
    it will show this error "The argument type 'DateTime' can't be assigned to the parameter type 'RepeatInterval'" – Kardo Aziz Jan 18 '22 at 20:26
0

always this approach works

  1. make your own copy of package
  2. modify it

1.make your own copy

you can easily copy the package file to your project . and use it like this (flutter doc)

dependencies:
  plugin1:
    path: ../plugin1/

if you prefer you can fork project and use it like below

dependencies:
  plugin1:
    git:
      url: git://github.com/flutter/plugin1.git

2.modify it

for your question you can change the value of Daily interval to (3 * Daily interval)

I found this part of code (android - ios)

Sajjad
  • 2,593
  • 16
  • 26
0

I would use the flutter cron package if I were you: cron package on pub.dev It allows you schedule a cron job which is simply a task that runs every x seconds or days, months... For your example:

fltrNotification = new FlutterLocalNotificationsPlugin();
final cron = Cron();
// Schedule a task that will run every 3 days
cron.schedule(Schedule.parse('0 0 */3 * *'), () async {
  // Schedule a notification right now
  fltrNotification.schedule(1, "Times Uppp", task, 
  DateTime.now(), generalNotificationDetails);
  print('every three days');
});
If you want to change the frequency, cron is very flexible and you can do pretty much any frequency, the cron syntax is pretty straightforward and their are some websites online that allow you to simply generate it.

There are, of course, several ways to use cron to do what you want. You could schedule a notification for the next 72 hours every 72 hours, refreshing every 24 hours, whatever seems better to you.

(I used part of Piyush Kumar's answer for this example by the way, and updated it to use cron)

jeremynac
  • 1,204
  • 3
  • 11