I would like to schedule daily notification on specific time (eg. 10:30PM daily). But I do not even receive one notification when I set the time to next minute of current time. This is what I do:
static Future<void> scheduleNotification({
int id = 0,
String? title,
String? body,
String? payLoad,
required DateTime scheduledNotificationDateTime,
}) async {
return _flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
_nextInstanceOfTime(scheduledNotificationDateTime),
notificationDetails(),
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
);
}
static tz.TZDateTime _nextInstanceOfTime(DateTime time) {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate = tz.TZDateTime(
tz.local, now.year, now.month, now.day, time.hour, time.minute);
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}
I have try to replace the _nextInstanceOfTime(scheduledNotificationDateTime)
to tz.TZDateTime.now(tz.local).add(const Duration(seconds: 1))
and I got notification after 1 second which my setup is correct to receive notification. Please help. Thank you in advanced
I have initialise the time zone database with
tz.initializeTimeZones()
but did not set the local location. So I added these line and now it is working already.