Search code examples
flutterpush-notificationnotificationsflutter-dependenciesandroid-notifications

How can I get a scheduled notification that repeats every day in Flutter?


I am using the flutter_local_notifications package and attempting to implement a notification system where I receive notifications daily at various times.

For example, one day I might receive a notification at 6:00 AM, the next day at 7:00 PM, and the following day at either 8:30 AM or 4:00 PM. However, notifications will occur daily.

I've tried utilizing both periodic notifications and scheduled notifications from flutter_local_notifications, but I'm struggling to understand how to achieve this.

This is my Notification Code

class LocalNotifications {
  static final FlutterLocalNotificationsPlugin
      _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
  static final onClickNotification = BehaviorSubject<String>();


  static void onNotificationTap(NotificationResponse notificationResponse) {
    onClickNotification.add(notificationResponse.payload!);
  }


  static Future init() async {
    const AndroidInitializationSettings initializationSettingsAndroid =
        AndroidInitializationSettings('@mipmap/ic_launcher');
    final DarwinInitializationSettings initializationSettingsDarwin =
        DarwinInitializationSettings(
      onDidReceiveLocalNotification: (id, title, body, payload) => null,
    );
    final LinuxInitializationSettings initializationSettingsLinux =
        LinuxInitializationSettings(defaultActionName: 'Open notification');
    final InitializationSettings initializationSettings =
        InitializationSettings(
            android: initializationSettingsAndroid,
            iOS: initializationSettingsDarwin,
            linux: initializationSettingsLinux);
    _flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onDidReceiveNotificationResponse: onNotificationTap,
        onDidReceiveBackgroundNotificationResponse: onNotificationTap);
  }
  static Future showPeriodicNotifications({
    required String title,
    required String body,
    required String payload,
  }) async {
    const AndroidNotificationDetails androidNotificationDetails =
        AndroidNotificationDetails('channel 2', 'your channel name',
            channelDescription: 'your channel description',
            importance: Importance.max,
            priority: Priority.high,
            ticker: 'ticker');
    const NotificationDetails notificationDetails =
        NotificationDetails(android: androidNotificationDetails);
    await _flutterLocalNotificationsPlugin.periodicallyShow(
      1,
      title,
      body,
      RepeatInterval.everyMinute,
      notificationDetails,
      payload: payload,
      androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
    );
  }


  static Future showScheduleNotification({
    required int id,
    required String title,
    required String body,
    required String primeTimes,
    required String payload,
  }) async {
    tz.initializeTimeZones();

    tz.TZDateTime payloadDateTime = tz.TZDateTime.parse(tz.local, primeTimes);

  
    await _flutterLocalNotificationsPlugin.zonedSchedule(
        id,
        title,
        body,
        payloadDateTime,
        const NotificationDetails(
            android: AndroidNotificationDetails(
          'channel 3',
          'your channel name',
          channelDescription: 'your channel description',
          importance: Importance.max,
          priority: Priority.high,
          ticker: 'ticker',
          styleInformation: BigTextStyleInformation(''),
        )),
        androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
        
        uiLocalNotificationDateInterpretation:
            UILocalNotificationDateInterpretation.absoluteTime,
        payload: payload, 
        
        );
  }

}

Solution

  • You need to pass a parameter in zonedSchedule with a value, to get scheduled notification daily at the given time.

    matchDateTimeComponents: DateTimeComponents.time
    

    According to the package, it states that

    If a value for matchDateTimeComponents parameter is given, this tells the plugin to schedule a recurring notification that matches the specified date and time components. Specifying DateTimeComponents.time would result in a daily notification at the same time whilst DateTimeComponents.dayOfWeekAndTime would result in a weekly notification that occurs on the same day of the week and time. This is similar to how recurring notifications on iOS/macOS work using a calendar trigger. Note that when a value is given, the scheduledDate may not represent the first time the notification will be shown. An example would be if the date and time is currently 2020-10-19 11:00 (i.e. 19th October 2020 11:00AM) and scheduledDate is 2020-10-21 10:00 and the value of the matchDateTimeComponents is DateTimeComponents.time, then the next time a notification will appear is 2020-10-20 10:00.