Class WebNotificationHandle
- All Implemented Interfaces:
Serializable
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 Summary
Modifier and TypeMethodDescriptionaddClickListener(SerializableRunnable listener) Adds a listener that runs when the user clicks the notification.addCloseListener(SerializableRunnable listener) Adds a listener that runs when the browser did not show the notification, for example because permission has not been granted.voidclose()Closes the notification if it is still visible.
-
Method Details
-
addClickListener
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, notnull- Returns:
- a registration for removing the listener
-
addCloseListener
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; seeaddErrorListener(SerializableConsumer).- Parameters:
listener- the listener, notnull- Returns:
- a registration for removing the listener
-
addErrorListener
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, notnull- 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.
-