我正在使用颤振本地通知,我想了解它的时间。在 date_time.dart 中,这是一个用于 Flutter 本地通知的代码文件,我发现:
"The hour of the day, expressed as in a 24-hour clock [0..23]."
这意味着我需要在早上 8 点创建通知,我应该输入代码 07。 但是flutter本地通知的例子,通知的意思是在上午10点,但在他们写的代码中,10.这意味着范围是[1..24],不是吗? 调度示例代码是:
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;
}
答案 0 :(得分:2)
此处通过在范围 [0..23] 的参数上传递 10 来获取上午 10 点的下一个实例
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;
}
因为这些值对应于 24 小时格式。 0 表示 00:00。和 23 表示 23:00,即分别为 12:00 AM 和 11:00 PM。
因此,要在下一个 8:00 AM 进行设置,您需要传递 8. 这意味着 24 小时的 8:00 和 12 小时格式的 8:00。