Reputation:
i am using flutter local notification, i want to understand the time on it. in the date_time.dart, which is a code file used in flutter local notification, i have found that:
"The hour of the day, expressed as in a 24-hour clock [0..23]."
that means that is i need to create a notification at 8 AM, i should type in code 07. but the example of flutter local notification, the notification meant to be in 10 AM, but in the code they wrote 10. which means that the range is [1..24], is not it? The scheduling example code is:
Future<void> _scheduleDailyTenAMNotification() async {
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
'daily scheduled notification title',
'daily scheduled notification body',
_nextInstanceOfTenAM(),
const NotificationDetails(
android: AndroidNotificationDetails(
'daily notification channel id',
'daily notification channel name',
'daily notification description'),
),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time);
}
tz.TZDateTime _nextInstanceOfTenAM() {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, 10);
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}
Upvotes: 0
Views: 1401
Reputation: 2007
Here Next Instance of 10 AM is fetched by passing 10 on the arguments ranging [0..23]
tz.TZDateTime _nextInstanceOfTenAM() {
final tz.TZDateTime now = tz.TZDateTime.now(tz.local);
tz.TZDateTime scheduledDate =
tz.TZDateTime(tz.local, now.year, now.month, now.day, 10); //here 10 is for 10:00 AM
if (scheduledDate.isBefore(now)) {
scheduledDate = scheduledDate.add(const Duration(days: 1));
}
return scheduledDate;
}
As the values correspond to 24 Hr format. 0 is for 00:00. and 23 is for 23:00, i.e 12:00 AM and 11:00 PM respectively.
So for setting at next instance of 8:00 AM you would need to pass 8. which will mean 8:00 in 24h and 8:00 AM in 12h format.
Upvotes: 2