scheduleEventNotification method

Future<void> scheduleEventNotification(
  1. ActivityModel event,
  2. int minutesInAdvance
)

Schedules a notification for an event.

Implementation

Future<void> scheduleEventNotification(
  ActivityModel event,
  int minutesInAdvance,
) async {
  final NotificationDetails platformChannelSpecifics = NotificationDetails(
    android: AndroidNotificationDetails(
      "event reminder",
      "Reminders for events",
      color: const Color(0xff006600),
      importance: Importance.max,
      priority: Priority.max,
      actions: [
        AndroidNotificationAction(
          "markAsRead",
          t.notifications.markAsRead,
        ),
      ],
    ),
    iOS: const DarwinNotificationDetails(),
  );

  tz.initializeTimeZones();
  final tz.TZDateTime scheduledDateTime =
      tz.TZDateTime.from(event.date.$1!, tz.getLocation("Europe/Amsterdam"))
          .subtract(Duration(minutes: minutesInAdvance));
  if (scheduledDateTime
      .isBefore(tz.TZDateTime.now(tz.getLocation('Europe/Amsterdam')))) {
    return;
  }
  final String day =
      minutesInAdvance == (24 * 60) ? t.time.tomorrow : t.time.today;
  final startTime = TimeOfDay.fromDateTime(event.date.$1!);
  await flutterLocalNotificationsPlugin.zonedSchedule(
    event.id.hashCode,
    t.notifications.title,
    "'${event.title!}' ${t.generics.starts} $day ${t.generics.at} ${startTime.toHHMMFormat()} ${t.generics.genericIn} ${event.location}",
    scheduledDateTime,
    platformChannelSpecifics,
    payload: EventRoute(id: event.id).location,
    uiLocalNotificationDateInterpretation:
        UILocalNotificationDateInterpretation.absoluteTime,
    androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
  );
  debugPrint(
    "Scheduled a notification for ${scheduledDateTime.toIso8601String()}",
  );
}