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:
1 Answer
Reset to default 0When you call DateTime.add
it returns a new DateTime object. From the docs:
Returns a new
DateTime
instance withduration
added to thisDateTime
.
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.
- Schedule a task for the first day that the notification needs to be displayed
- 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
版权声明:本文标题:How to schedule flutter local notification to repeat every day starting from tomorrow? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744673384a2618962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
flutter_local_notifications
, but did you consider passing thetzDateTimeNow
of tomorrow? – Frank van Puffelen Commented Mar 14 at 15:28const Duration(days: 1)
, it will be converted to tomorrow's date. But asmatchDateTimeComponents
equalsDateTimeComponents.time
, it also executes on today's time. – dipansh Commented Mar 14 at 15:35