admin管理员组

文章数量:1391947

I'm trying to set up flutter notification that appear initially tomorrow, and then repeat daily after that using /packages/flutter_local_notifications. This is my current approach:

// method to schedule a notification
  static Future<void> scheduleNotification({
    required int id,
    required String title,
    required String body,
    required tz.TZDateTime tzDateTime,
    bool repeatDaily = false,
  }) async {
    // schedule a notification
    await _flutterLocalNotificationsPlugin.zonedSchedule(
      id,
      title,
      body,
      tzDateTime,
      NotificationDetails(android: _androidNotificationDetails),
      // if repeat daily, then we only compare the time, otherwise we set it to check date and time
      matchDateTimeComponents: repeatDaily
          ? DateTimeComponents.time
          : DateTimeComponents.dateAndTime,
      // ios specific
      uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.absoluteTime,
      // android specific
      androidScheduleMode: AndroidScheduleMode.inexactAllowWhileIdle,
    );
  }

// scheduling the notification
scheduleNotification(
        id: assignmentReminder.id,
        title: assignmentReminder.title,
        body: assignmentReminder.body,
        tzDateTime: tz.TZDateTime(
          tz.local,
          tzDateTimeNow.year,
          tzDateTimeNow.month,
          tzDateTimeNow.day,
          assignmentReminderTime.hour,
          assignmentReminderTime.minute,
        ).add(const Duration(days: 1)),
        repeatDaily: true,
      );

But it is repeating today as well. I don't want it to execute today no matter what. Any ideas on this please?

Another way I tried to do this:

final tomorrowDate = tzDateTimeNow.add(Duration(days: 1));

      print(tomorrowDate);

      // scheduling for this day
      scheduleNotification(
        id: 1,
        title: assignmentReminder.title,
        body: assignmentReminder.body,
        tzDateTime: tz.TZDateTime(
          tz.local,
          tomorrowDate.year,
          tomorrowDate.month,
          tomorrowDate.day,
          assignmentReminderTime.hour,
          assignmentReminderTime.minute,
        ),
        repeatDaily: true,
      );

Output for tomorrowDate. Today is 16 March 2025 for me:

I'm trying to set up flutter notification that appear initially tomorrow, and then repeat daily after that using https://pub.dev/packages/flutter_local_notifications. This is my current approach:

// method to schedule a notification
  static Future<void> scheduleNotification({
    required int id,
    required String title,
    required String body,
    required tz.TZDateTime tzDateTime,
    bool repeatDaily = false,
  }) async {
    // schedule a notification
    await _flutterLocalNotificationsPlugin.zonedSchedule(
      id,
      title,
      body,
      tzDateTime,
      NotificationDetails(android: _androidNotificationDetails),
      // if repeat daily, then we only compare the time, otherwise we set it to check date and time
      matchDateTimeComponents: repeatDaily
          ? DateTimeComponents.time
          : DateTimeComponents.dateAndTime,
      // ios specific
      uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.absoluteTime,
      // android specific
      androidScheduleMode: AndroidScheduleMode.inexactAllowWhileIdle,
    );
  }

// scheduling the notification
scheduleNotification(
        id: assignmentReminder.id,
        title: assignmentReminder.title,
        body: assignmentReminder.body,
        tzDateTime: tz.TZDateTime(
          tz.local,
          tzDateTimeNow.year,
          tzDateTimeNow.month,
          tzDateTimeNow.day,
          assignmentReminderTime.hour,
          assignmentReminderTime.minute,
        ).add(const Duration(days: 1)),
        repeatDaily: true,
      );

But it is repeating today as well. I don't want it to execute today no matter what. Any ideas on this please?

Another way I tried to do this:

final tomorrowDate = tzDateTimeNow.add(Duration(days: 1));

      print(tomorrowDate);

      // scheduling for this day
      scheduleNotification(
        id: 1,
        title: assignmentReminder.title,
        body: assignmentReminder.body,
        tzDateTime: tz.TZDateTime(
          tz.local,
          tomorrowDate.year,
          tomorrowDate.month,
          tomorrowDate.day,
          assignmentReminderTime.hour,
          assignmentReminderTime.minute,
        ),
        repeatDaily: true,
      );

Output for tomorrowDate. Today is 16 March 2025 for me:

Share Improve this question edited Mar 16 at 10:02 dipansh asked Mar 14 at 4:52 dipanshdipansh 5309 silver badges25 bronze badges 2
  • I'm not a user of flutter_local_notifications, but did you consider passing the tzDateTimeNow of tomorrow? – Frank van Puffelen Commented Mar 14 at 15:28
  • When I'm adding const Duration(days: 1), it will be converted to tomorrow's date. But as matchDateTimeComponents equals DateTimeComponents.time, it also executes on today's time. – dipansh Commented Mar 14 at 15:35
Add a comment  | 

1 Answer 1

Reset to default 0

When you call DateTime.add it returns a new DateTime object. From the docs:

Returns a new DateTime instance with duration added to this DateTime.

So when you this add(const Duration(days: 1)), it returns a new object. But inside the tz.TZDateTime( here, you're still using the previous (unmodified) DateTime object:

tz.TZDateTime(
   tz.local,
   tzDateTimeNow.year,
   tzDateTimeNow.month,
   tzDateTimeNow.day,
   assignmentReminderTime.hour,
   assignmentReminderTime.minute,
).add(const Duration(days: 1))

You'll want to extract the .add(const Duration(days: 1)) out of this expression, assign the resulting (new) object to a separate variable, and then use that in the call to tz.TZDateTime(...).


Update: I also noted this in your call:

matchDateTimeComponents: repeatDaily
          ? DateTimeComponents.time
          : DateTimeComponents.dateAndTime,

So for daily notifications, you're only using the time part of the DateTime - ignoring the date itself completely. So that perfectly explains why the notification also shows today already, as you're telling the library to ignore the date.

If this is the only way to get a repeating notification with flutter_local_notifications, you may have to take a two-pronged approach.

  1. Schedule a task for the first day that the notification needs to be displayed
  2. In that task, schedule the recurring notification as you do now

本文标签: How to schedule flutter local notification to repeat every day starting from tomorrowStack Overflow