showNotification static method

Future<void> showNotification(
  1. String title, {
  2. String? dir,
  3. String? body,
  4. String? lang,
  5. String? tag,
  6. String? icon,
  7. List<WebNotificationAction> actions = const [],
})

Shows a notification via "Notification API" of the browser.

Implementation

static Future<void> showNotification(
  String title, {
  String? dir,
  String? body,
  String? lang,
  String? tag,
  String? icon,
  List<WebNotificationAction> actions = const [],
}) async {
  final options = web.NotificationOptions();

  if (dir != null) {
    options.dir = dir;
  }
  if (body != null) {
    options.body = body;
  }
  if (lang != null) {
    options.lang = lang;
  }
  if (tag != null) {
    options.tag = tag;
  }
  if (icon != null) {
    options.icon = icon;
  }
  if (actions.isNotEmpty) {
    options.actions = actions
        .map(
          (e) => web.NotificationAction(
            action: e.id,
            title: e.title,
            icon: e.icon ?? '',
          ),
        )
        .toList()
        .toJS;
  }

  // TODO: `onSelectNotification` was used in `onclick` event in
  //       `Notification` body, however since now notifications are created
  //       by `ServiceWorker`, we have no control over it, so should implement
  //       a way to handle the click in `ServiceWorker`.
  final web.ServiceWorkerRegistration registration =
      await web.window.navigator.serviceWorker.ready.toDart;

  await registration.showNotification(title, options).toDart;
}