Class WebNotification

java.lang.Object
com.vaadin.flow.component.webnotification.WebNotification
All Implemented Interfaces:
Serializable

public final class WebNotification extends Object implements Serializable
Entry point for the browser's Notifications API, which shows native operating system notifications while the application is open in a browser tab. Use it to tell the user that something happened while they are looking at another tab or application — a long-running job finished, a message arrived.

 Button enable = new Button("Enable notifications");
 WebNotification.onClick(enable).requestPermission();
 Signal.effect(enable, () -> enable.setVisible(WebNotification
         .permissionSignal().get() == WebNotificationPermission.PROMPT));

 // Later, for example from a background job through ui.access(...)
 if (WebNotification.permissionSignal(ui)
         .peek() == WebNotificationPermission.GRANTED) {
     WebNotification
             .show("Report ready",
                     WebNotificationOptions.builder()
                             .body("The March report finished").build(),
                     ui)
             .addClickListener(() -> ui.navigate(ReportView.class));
 } else {
     Notification.show("The March report finished");
 }
 
To notify only while the user is away from the tab, check ui.getPage().pageVisibilitySignal() before showing a notification.

Browser caveats.

  • The Notifications API requires a secure context (HTTPS or localhost); elsewhere the permission is WebNotificationPermission.UNSUPPORTED.
  • Chrome on Android only shows notifications through a service worker, so show(String) reports a WebNotificationError.Reason.UNKNOWN error there although the permission can be granted.
  • Notifications are only shown while the page is open. To reach users when the application is closed, use Web Push instead.
See Also:
  • Method Details

    • onClick

      public static WebNotificationBinding onClick(Component component)
      Starts a notification action bound to clicks on the given component — the entry point for "Enable notifications" buttons. Chain the action onto the returned WebNotificationBinding:
      
       Button enable = new Button("Enable notifications");
       WebNotification.onClick(enable).requestPermission();
       
      The browser only shows the permission prompt while it is handling a genuine user gesture, and that activation is gone by the time a normal server-side click listener runs. Binding the action here makes it run inside the browser's own click handler, where the gesture is still valid — which is why permission is requested through onClick rather than an ordinary addClickListener.
      Parameters:
      component - the component whose clicks trigger the action, not null
      Returns:
      a binding for chaining the action to perform on click
    • permissionSignal

      public static Signal<WebNotificationPermission> permissionSignal()
      Returns a read-only signal telling whether the browser lets the current UI show notifications.

      The value is seeded during bootstrap and follows the user's decisions afterwards: it changes when the user answers the permission prompt, and when the user changes the setting in the browser's site settings — at the latest when they return to the tab. Reading it never shows a prompt.

      Use Signal.effect(owner, ...) to react to changes and .peek() for a snapshot outside a reactive context.

      Returns:
      the read-only permission signal
      Throws:
      IllegalStateException - if there is no current UI
      UIDetachedException - if the current UI is not attached to a session
    • permissionSignal

      public static Signal<WebNotificationPermission> permissionSignal(UI ui)
      Returns a read-only signal telling whether the browser lets the given UI show notifications. Same semantics as permissionSignal(); use this overload from background threads or anywhere UI.getCurrent() is unreliable.
      Parameters:
      ui - the UI to read the signal from, not null
      Returns:
      the read-only permission signal
      Throws:
      UIDetachedException - if the UI is not attached to a session
    • show

      public static WebNotificationHandle show(String title)
      Shows a notification with the given title on the current UI. Needs no user gesture, but the user must have granted permission; otherwise the error listeners of the returned handle receive WebNotificationError.Reason.NOT_ALLOWED.
      Parameters:
      title - the title of the notification, not null
      Returns:
      a handle for reacting to clicks and closing the notification
      Throws:
      IllegalStateException - if there is no current UI
    • show

      public static WebNotificationHandle show(String title, UI ui)
      Shows a notification with the given title on the given UI. Same as show(String); use this overload from background threads or anywhere UI.getCurrent() is unreliable. Like any change to the UI, the call must hold the session lock, so call it inside ui.access(...) from a background thread.
      Parameters:
      title - the title of the notification, not null
      ui - the UI to show the notification through, not null
      Returns:
      a handle for reacting to clicks and closing the notification
    • show

      public static WebNotificationHandle show(String title, WebNotificationOptions options)
      Shows a notification with the given title on the current UI, with a body text, an icon, a tag or other options. Same as show(String) otherwise.
      Parameters:
      title - the title of the notification, not null
      options - what the notification shows besides its title, and how it behaves, not null
      Returns:
      a handle for reacting to clicks and closing the notification
      Throws:
      IllegalStateException - if there is no current UI
    • show

      public static WebNotificationHandle show(String title, WebNotificationOptions options, UI ui)
      Shows a notification with the given title on the given UI, with a body text, an icon, a tag or other options. Same as show(String, WebNotificationOptions); use this overload from background threads or anywhere UI.getCurrent() is unreliable. Like any change to the UI, the call must hold the session lock, so call it inside ui.access(...) from a background thread.
      Parameters:
      title - the title of the notification, not null
      options - what the notification shows besides its title, and how it behaves, not null
      ui - the UI to show the notification through, not null
      Returns:
      a handle for reacting to clicks and closing the notification
    • closeByTag

      public static void closeByTag(String tag)
      Closes the notifications with the given tag that the current UI has shown and that are still visible. Use it to withdraw a notification when whatever it was about has been handled in the application.
      Parameters:
      tag - the tag of the notifications to close, not null
      Throws:
      IllegalStateException - if there is no current UI
    • closeByTag

      public static void closeByTag(String tag, UI ui)
      Closes the notifications with the given tag that the given UI has shown and that are still visible. Same as closeByTag(String); use this overload from background threads or anywhere UI.getCurrent() is unreliable. Like any change to the UI, the call must hold the session lock, so call it inside ui.access(...) from a background thread.
      Parameters:
      tag - the tag of the notifications to close, not null
      ui - the UI that showed the notifications, not null