我正在制作水提醒,需要在上午 8 点、中午 12 点、下午 4 点和晚上 8 点发送通知。
这是我的代码,当我单击通知按钮时,它工作正常,我唯一的问题是重复,即使在研究之后我也不知道如何去做我想做的事情。
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: 'high_importance_channel',
title: title,
body: body,
actionType: actionType,
notificationLayout: notificationLayout,
summary: summary,
category: category,
payload: payload,
bigPicture: bigPicture,
),
actionButtons: actionButtons,
schedule: scheduled ? NotificationInterval(
interval: interval,
timeZone: await AwesomeNotifications().getLocalTimeZoneIdentifier(),
preciseAlarm: true,
)
: null,
);
我找到了解决这个问题的方法,方法是在所需的时间设置不同的通知方法,每次调用
Future <void> showScheduledNotification8AM({
int id = 8,
String? title,
String? body,
String? summary,
Map <String, String>? payload,
ActionType actionType = ActionType.Default,
NotificationLayout notificationLayout = NotificationLayout.Default,
NotificationCategory? category,
String? bigPicture,
List <NotificationActionButton>? actionButtons,
bool? scheduled,
int? interval,
int? scheduledTime = 8,
}) async {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: 'scheduled',
title: title,
body: body,
actionType: actionType,
notificationLayout: notificationLayout,
summary: summary,
category: category,
payload: payload,
bigPicture: bigPicture,
),
actionButtons: actionButtons,
schedule: NotificationCalendar(hour: scheduledTime, minute: 0, repeats: false,)
);
}
Future <void> showScheduledNotification12PM({
int id = 12,
String? title,
String? body,
String? summary,
Map <String, String>? payload,
ActionType actionType = ActionType.Default,
NotificationLayout notificationLayout = NotificationLayout.Default,
NotificationCategory? category,
String? bigPicture,
List <NotificationActionButton>? actionButtons,
bool? scheduled,
int? interval,
int? scheduledTime = 12,
}) async {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: 'scheduled',
title: title,
body: body,
actionType: actionType,
notificationLayout: notificationLayout,
summary: summary,
category: category,
payload: payload,
bigPicture: bigPicture,
),
actionButtons: actionButtons,
schedule: NotificationCalendar(hour: scheduledTime, minute: 0, repeats: false,)
);
}
Future <void> showScheduledNotification4PM({
int id = 16,
String? title,
String? body,
String? summary,
Map <String, String>? payload,
ActionType actionType = ActionType.Default,
NotificationLayout notificationLayout = NotificationLayout.Default,
NotificationCategory? category,
String? bigPicture,
List <NotificationActionButton>? actionButtons,
bool? scheduled,
int? interval,
int? scheduledTime = 16,
}) async {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: 'scheduled',
title: title,
body: body,
actionType: actionType,
notificationLayout: notificationLayout,
summary: summary,
category: category,
payload: payload,
bigPicture: bigPicture,
),
actionButtons: actionButtons,
schedule: NotificationCalendar(hour: scheduledTime, minute: 32, repeats: false,)
);
}
Future <void> showScheduledNotification8PM({
int id = 20,
String? title,
String? body,
String? summary,
Map <String, String>? payload,
ActionType actionType = ActionType.Default,
NotificationLayout notificationLayout = NotificationLayout.Default,
NotificationCategory? category,
String? bigPicture,
List <NotificationActionButton>? actionButtons,
bool? scheduled,
int? interval,
int? scheduledTime = 20,
}) async {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: id,
channelKey: 'scheduled',
title: title,
body: body,
actionType: actionType,
notificationLayout: notificationLayout,
summary: summary,
category: category,
payload: payload,
bigPicture: bigPicture,
),
actionButtons: actionButtons,
schedule: NotificationCalendar(hour: scheduledTime, minute: 0, repeats: false,)
);
}
并使用onChange函数来调用每个方法。