scheduleAlarmNotif method

Future<void> scheduleAlarmNotif({
  1. required int id,
  2. required DateTime dateTime,
  3. required String title,
  4. required String body,
})

Schedules notification at the given dateTime.

Implementation

Future<void> scheduleAlarmNotif({
  required int id,
  required DateTime dateTime,
  required String title,
  required String body,
}) async {
  const iOSPlatformChannelSpecifics = DarwinNotificationDetails(
    presentSound: false,
    presentAlert: false,
    presentBadge: false,
  );

  const androidPlatformChannelSpecifics = AndroidNotificationDetails(
    'alarm',
    'alarm_plugin',
    channelDescription: 'Alarm plugin',
    importance: Importance.max,
    priority: Priority.max,
    enableLights: true,
    playSound: false,
  );

  const platformChannelSpecifics = NotificationDetails(
    iOS: iOSPlatformChannelSpecifics,
    android: androidPlatformChannelSpecifics,
  );

  final zdt = nextInstanceOfTime(
    Time(
      dateTime.hour,
      dateTime.minute,
      dateTime.second,
    ),
  );

  final hasPermission = await requestPermission();
  if (!hasPermission) {
    alarmPrint('Notification permission not granted');
    return;
  }

  try {
    await localNotif.zonedSchedule(
      id,
      title,
      body,
      tz.TZDateTime.from(zdt.toUtc(), tz.UTC),
      platformChannelSpecifics,
      androidAllowWhileIdle: true,
      uiLocalNotificationDateInterpretation:
          UILocalNotificationDateInterpretation.absoluteTime,
    );
    alarmPrint('Notification with id $id scheduled successfuly at $zdt');
  } catch (e) {
    alarmPrint('Schedule notification with id $id error: $e');
  }
}