zonedSchedule method

Future<void> zonedSchedule(
  1. int id,
  2. String? title,
  3. String? body,
  4. TZDateTime scheduledDate,
  5. NotificationDetails notificationDetails, {
  6. required AndroidScheduleMode androidScheduleMode,
  7. String? payload,
  8. 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(
  int id,
  String? title,
  String? body,
  TZDateTime scheduledDate,
  NotificationDetails notificationDetails, {
  required AndroidScheduleMode androidScheduleMode,
  String? payload,
  DateTimeComponents? matchDateTimeComponents,
}) async {
  if (kIsWeb) {
    return;
  }
  if (defaultTargetPlatform == TargetPlatform.android) {
    await resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()!
        .zonedSchedule(
            id, title, body, scheduledDate, notificationDetails.android,
            payload: payload,
            scheduleMode: androidScheduleMode,
            matchDateTimeComponents: matchDateTimeComponents);
  } else if (defaultTargetPlatform == TargetPlatform.iOS) {
    await resolvePlatformSpecificImplementation<
            IOSFlutterLocalNotificationsPlugin>()
        ?.zonedSchedule(
            id, title, body, scheduledDate, notificationDetails.iOS,
            payload: payload,
            matchDateTimeComponents: matchDateTimeComponents);
  } else if (defaultTargetPlatform == TargetPlatform.macOS) {
    await resolvePlatformSpecificImplementation<
            MacOSFlutterLocalNotificationsPlugin>()
        ?.zonedSchedule(
            id, title, body, scheduledDate, notificationDetails.macOS,
            payload: payload,
            matchDateTimeComponents: matchDateTimeComponents);
  } else if (defaultTargetPlatform == TargetPlatform.windows) {
    await resolvePlatformSpecificImplementation<
            FlutterLocalNotificationsWindows>()
        ?.zonedSchedule(
      id,
      title,
      body,
      scheduledDate,
      notificationDetails.windows,
      payload: payload,
    );
  } else {
    throw UnimplementedError('zonedSchedule() has not been implemented');
  }
}