我正在使用 flutter_local_notifications 包并尝试实现一个通知系统,我每天都会在不同时间收到通知。
例如,有一天我可能会在上午 6:00、第二天晚上 7:00 以及第二天上午 8:30 或下午 4:00 收到通知。但是,通知每天都会发生。
我尝试使用来自 flutter_local_notifications 的定期通知和计划通知,但我很难理解如何实现这一点。
这是我的通知代码
class LocalNotifications {
static final FlutterLocalNotificationsPlugin
_flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
static final onClickNotification = BehaviorSubject<String>();
static void onNotificationTap(NotificationResponse notificationResponse) {
onClickNotification.add(notificationResponse.payload!);
}
static Future init() async {
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
final DarwinInitializationSettings initializationSettingsDarwin =
DarwinInitializationSettings(
onDidReceiveLocalNotification: (id, title, body, payload) => null,
);
final LinuxInitializationSettings initializationSettingsLinux =
LinuxInitializationSettings(defaultActionName: 'Open notification');
final InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsDarwin,
linux: initializationSettingsLinux);
_flutterLocalNotificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse: onNotificationTap,
onDidReceiveBackgroundNotificationResponse: onNotificationTap);
}
static Future showPeriodicNotifications({
required String title,
required String body,
required String payload,
}) async {
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails('channel 2', 'your channel name',
channelDescription: 'your channel description',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker');
const NotificationDetails notificationDetails =
NotificationDetails(android: androidNotificationDetails);
await _flutterLocalNotificationsPlugin.periodicallyShow(
1,
title,
body,
RepeatInterval.everyMinute,
notificationDetails,
payload: payload,
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
);
}
static Future showScheduleNotification({
required int id,
required String title,
required String body,
required String primeTimes,
required String payload,
}) async {
tz.initializeTimeZones();
tz.TZDateTime payloadDateTime = tz.TZDateTime.parse(tz.local, primeTimes);
await _flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
payloadDateTime,
const NotificationDetails(
android: AndroidNotificationDetails(
'channel 3',
'your channel name',
channelDescription: 'your channel description',
importance: Importance.max,
priority: Priority.high,
ticker: 'ticker',
styleInformation: BigTextStyleInformation(''),
)),
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
payload: payload,
);
}
}
您需要在 zonedSchedule 中传递一个带有值的参数,以便每天在给定时间获取预定通知。
matchDateTimeComponents: DateTimeComponents.time
根据包装上写着
如果给定了 matchDateTimeComponents 参数的值,则表明 该插件用于安排与以下内容匹配的定期通知 指定的日期和时间组件。指定 DateTimeComponents.time 会同时产生每日通知 DateTimeComponents.dayOfWeekAndTime 将导致每周 在一周的同一天和同一时间发生的通知。这是 类似于 iOS/macOS 上的循环通知如何使用 日历触发器。请注意,当给定值时,scheduledDate 可能并不代表第一次显示通知。一个 例如,如果日期和时间当前为 2020-10-19 11:00 (即2020年10月19日上午11:00)预定日期为2020年10月21日10:00 matchDateTimeComponents 的值为 DateTimeComponents.time,那么下次会出现通知 是 2020 年 10 月 20 日 10:00。