scheduleNotification method

  1. @override
Future<bool> scheduleNotification({
  1. required int id,
  2. required String title,
  3. required String message,
  4. required int scheduledEpochMillis,
  5. String? channelId,
  6. NotificationImportance? importance,
  7. bool alarmSound = false,
  8. String? targetScreen,
  9. Map<String, dynamic>? extraData,
})
override

Schedule a notification to be delivered by the operating system at a specific point in time, even when the app is fully closed.

Implementation

@override
Future<bool> scheduleNotification({
  required int id,
  required String title,
  required String message,
  required int scheduledEpochMillis,
  String? channelId,
  NotificationImportance? importance,
  bool alarmSound = false,
  String? targetScreen,
  Map<String, dynamic>? extraData,
}) async {
  try {
    _scheduledTimers[id]?.cancel();
    final delay =
        scheduledEpochMillis - DateTime.now().millisecondsSinceEpoch;
    if (delay <= 0) {
      // Deliver immediately if the time has already passed.
      if (web.Notification.permission == 'granted') {
        web.Notification(title, web.NotificationOptions(body: message));
      }
      return true;
    }
    _scheduledTimers[id] = Timer(Duration(milliseconds: delay), () {
      if (web.Notification.permission == 'granted') {
        web.Notification(title, web.NotificationOptions(body: message));
      }
      _scheduledTimers.remove(id);
    });
    return true;
  } catch (_) {
    return false;
  }
}