zonedSchedule method
- required int id,
- required TZDateTime scheduledDate,
- required NotificationDetails notificationDetails,
- required AndroidScheduleMode androidScheduleMode,
- String? title,
- String? body,
- String? payload,
- DateTimeComponents? matchDateTimeComponents,
Schedules a notification to be shown at the specified date and time relative to a specific time zone.
Note that to get the appropriate representation of the time at the native level (i.e. Android/iOS), the plugin needs to pass the time over the platform channel in yyyy-mm-dd hh:mm:ss format. Therefore, the precision is at the best to the second.
If a value for matchDateTimeComponents parameter is given, this tells
the plugin to schedule a recurring notification that matches the
specified date and time components. Specifying
DateTimeComponents.time would result in a daily notification at the
same time whilst DateTimeComponents.dayOfWeekAndTime would result
in a weekly notification that occurs on the same day of the week and time.
This is similar to how recurring notifications on iOS/macOS work using a
calendar trigger. Note that when a value is given, the scheduledDate
may not represent the first time the notification will be shown. An
example would be if the date and time is currently 2020-10-19 11:00
(i.e. 19th October 2020 11:00AM) and scheduledDate is 2020-10-21
10:00 and the value of the matchDateTimeComponents is
DateTimeComponents.time, then the next time a notification will
appear is 2020-10-20 10:00.
On Android, this will also require additional setup for the app,
especially in the app's AndroidManifest.xml file. Please see check the
readme for further details.
On Windows, this will only set a notification on the scheduledDate, and
not repeat, regardless of the value for matchDateTimeComponents.
Implementation
Future<void> zonedSchedule({
required int id,
required TZDateTime scheduledDate,
required NotificationDetails notificationDetails,
required AndroidScheduleMode androidScheduleMode,
String? title,
String? body,
String? payload,
DateTimeComponents? matchDateTimeComponents,
}) async {
if (kIsWeb) {
await resolvePlatformSpecificImplementation<
WebFlutterLocalNotificationsPlugin
>()
?.zonedSchedule(
id: id,
title: title,
body: body,
scheduledDate: scheduledDate,
matchDateTimeComponents: matchDateTimeComponents,
payload: payload,
);
return;
}
if (defaultTargetPlatform == TargetPlatform.android) {
await resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin
>()!
.zonedSchedule(
id: id,
title: title,
body: body,
scheduledDate: scheduledDate,
notificationDetails: notificationDetails.android,
payload: payload,
scheduleMode: androidScheduleMode,
matchDateTimeComponents: matchDateTimeComponents,
);
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
await resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin
>()
?.zonedSchedule(
id: id,
title: title,
body: body,
scheduledDate: scheduledDate,
notificationDetails: notificationDetails.iOS,
payload: payload,
matchDateTimeComponents: matchDateTimeComponents,
);
} else if (defaultTargetPlatform == TargetPlatform.macOS) {
await resolvePlatformSpecificImplementation<
MacOSFlutterLocalNotificationsPlugin
>()
?.zonedSchedule(
id: id,
title: title,
body: body,
scheduledDate: scheduledDate,
notificationDetails: notificationDetails.macOS,
payload: payload,
matchDateTimeComponents: matchDateTimeComponents,
);
} else if (defaultTargetPlatform == TargetPlatform.windows) {
await resolvePlatformSpecificImplementation<
FlutterLocalNotificationsWindows
>()
?.zonedSchedule(
id: id,
title: title,
body: body,
scheduledDate: scheduledDate,
notificationDetails: notificationDetails.windows,
payload: payload,
);
} else {
await FlutterLocalNotificationsPlatform.instance.zonedSchedule(
id: id,
title: title,
body: body,
scheduledDate: scheduledDate,
matchDateTimeComponents: matchDateTimeComponents,
);
}
}