Class WebNotificationHandle

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

public final class WebNotificationHandle extends Object implements Serializable
A notification shown with WebNotification.show(java.lang.String). Use it to react when the user clicks the notification, to learn when it is gone, and to close it from the server:

 WebNotificationHandle notification = WebNotification.show("Report ready");
 notification.addClickListener(() -> ui.navigate(ReportView.class));
 
The browser reports what happens to the notification asynchronously, so a listener added right after show never misses an event. Every listener runs on the UI thread.

The handle reports what happens to the notification until it is closed or fails to show. A notification the user neither clicks nor dismisses can stay in the operating system's notification center for a long time, and the browser does not always report when it is gone, so a UI tracks at most the 50 most recently shown notifications: showing another one closes the oldest, and its close listeners run. A handle also stops being tracked when its UI is closed, for example on a page reload; its close listeners then run as well, although the notification may still be on screen, since nothing can be reported from it anymore. After that no listener runs again and close() does nothing. Call close() when a notification is no longer relevant, or use a tag so the next notification replaces it.

See Also:
  • Method Details

    • addClickListener

      public Registration addClickListener(SerializableRunnable listener)
      Adds a listener that runs when the user clicks the notification. The browser brings the tab to the front and closes the notification before the listener runs, so the listener typically navigates to whatever the notification was about.
      Parameters:
      listener - the listener, not null
      Returns:
      a registration for removing the listener
    • addCloseListener

      public Registration addCloseListener(SerializableRunnable listener)
      Adds a listener that runs when the notification is gone: the user dismissed it or clicked it, close() closed it, or a notification with the same tag replaced it. Also runs when the UI closes the notification because it has shown too many newer ones, and when the UI is closed; see the class documentation. Not called when the notification failed to show; see addErrorListener(SerializableConsumer).
      Parameters:
      listener - the listener, not null
      Returns:
      a registration for removing the listener
    • addErrorListener

      public Registration addErrorListener(SerializableConsumer<WebNotificationError> listener)
      Adds a listener that runs when the browser did not show the notification, for example because permission has not been granted. Use it to fall back to an in-page message.
      Parameters:
      listener - the listener receiving why the notification was not shown, not null
      Returns:
      a registration for removing the listener
    • close

      public void close()
      Closes the notification if it is still visible. The close listeners run once the browser confirms. Calling this when the notification is already gone, or when the handle no longer tracks it, does nothing.

      Like any change to the UI, the call must hold the session lock, so call it inside ui.access(...) from a background thread.