zonedSchedule method

Future<void> zonedSchedule(
  1. int id,
  2. String? title,
  3. String? body,
  4. TZDateTime scheduledDate,
  5. NotificationDetails notificationDetails,
  6. {required UILocalNotificationDateInterpretation uiLocalNotificationDateInterpretation,
  7. @Deprecated('Deprecated in favor of the androidScheduleMode parameter') bool androidAllowWhileIdle = false,
  8. AndroidScheduleMode? androidScheduleMode,
  9. String? payload,
  10. 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.

The androidAllowWhileIdle parameter determines if the notification should still be shown at the exact time even when the device is in a low-power idle mode. This parameter has been deprecated and will removed in a future major release in favour of the androidScheduledMode parameter that provides the same functionality in addition to being able to schedule notifications with inexact timings.

The uiLocalNotificationDateInterpretation is for iOS versions older than 10 as the APIs have limited support for time zones. With this parameter, it is used to determine if the scheduled date should be interpreted as absolute time or wall clock time.

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.

Implementation

Future<void> zonedSchedule(
  int id,
  String? title,
  String? body,
  TZDateTime scheduledDate,
  NotificationDetails notificationDetails, {
  required UILocalNotificationDateInterpretation
      uiLocalNotificationDateInterpretation,
  @Deprecated('Deprecated in favor of the androidScheduleMode parameter')
  bool androidAllowWhileIdle = false,
  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: _chooseScheduleMode(
                androidScheduleMode, androidAllowWhileIdle),
            matchDateTimeComponents: matchDateTimeComponents);
  } else if (defaultTargetPlatform == TargetPlatform.iOS) {
    await resolvePlatformSpecificImplementation<
            IOSFlutterLocalNotificationsPlugin>()
        ?.zonedSchedule(
            id, title, body, scheduledDate, notificationDetails.iOS,
            uiLocalNotificationDateInterpretation:
                uiLocalNotificationDateInterpretation,
            payload: payload,
            matchDateTimeComponents: matchDateTimeComponents);
  } else if (defaultTargetPlatform == TargetPlatform.macOS) {
    await resolvePlatformSpecificImplementation<
            MacOSFlutterLocalNotificationsPlugin>()
        ?.zonedSchedule(
            id, title, body, scheduledDate, notificationDetails.macOS,
            payload: payload,
            matchDateTimeComponents: matchDateTimeComponents);
  } else {
    throw UnimplementedError('zonedSchedule() has not been implemented');
  }
}