scheduleTaskNotification method

  1. @override
Future<void> scheduleTaskNotification(
  1. UserTask task
)
override

Schedule a notification for a task at the task.triggerTime.

Implementation

@override
Future<void> scheduleTaskNotification(UserTask task) async {
  // early out if task should not create a notification when scheduled
  if (!task.notification) return;

  if (task.triggerTime.isAfter(DateTime.now())) {
    tz.initializeTimeZones(); // for some strange reason, the time zones are not always initialized when this method is called, so we initialize them here to be sure

    final time = tz.TZDateTime.from(
      task.triggerTime,
      tz.getLocation(Settings().timezone),
    );

    await FlutterLocalNotificationsPlugin().zonedSchedule(
      id: task.id.hashCode,
      title: task.title,
      body: task.description,
      scheduledDate: time,
      notificationDetails: _platformChannelSpecifics,
      androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
      payload: task.id,
    );
    task.hasNotificationBeenCreated = true;
    debug('$runtimeType - Notification scheduled for $task at $time');
  } else {
    warning(
      '$runtimeType - Can only schedule a notification in the future. '
      'task trigger time: ${task.triggerTime}.',
    );
  }
}