Class WebNotification
java.lang.Object
com.vaadin.flow.component.webnotification.WebNotification
- All Implemented Interfaces:
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.
onClick(Component)— ask for permission when the user clicks a component. Browsers only show the permission prompt during a user gesture, so this is the only reliable way to ask.permissionSignal()— whether notifications are allowed, kept up to date when the user changes their mind.show(String, WebNotificationOptions)— show a notification and get aWebNotificationHandlefor reacting to clicks.
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 isWebNotificationPermission.UNSUPPORTED. - Chrome on Android only shows notifications through a service worker, so
show(String)reports aWebNotificationError.Reason.UNKNOWNerror 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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceWhat web notifications ask of their client-side bridge, as a JavaScript definition forElement.executeJs(Class). -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcloseByTag(String tag) Closes the notifications with the given tag that the current UI has shown and that are still visible.static voidcloseByTag(String tag, UI ui) Closes the notifications with the given tag that the given UI has shown and that are still visible.static WebNotificationBindingStarts a notification action bound to clicks on the given component — the entry point for "Enable notifications" buttons.static Signal<WebNotificationPermission> Returns a read-only signal telling whether the browser lets the current UI show notifications.static Signal<WebNotificationPermission> permissionSignal(UI ui) Returns a read-only signal telling whether the browser lets the given UI show notifications.static WebNotificationHandleShows a notification with the given title on the current UI.static WebNotificationHandleShows a notification with the given title on the given UI.static WebNotificationHandleshow(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.static WebNotificationHandleshow(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.
-
Method Details
-
onClick
Starts a notification action bound to clicks on the given component — the entry point for "Enable notifications" buttons. Chain the action onto the returnedWebNotificationBinding:
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 throughButton enable = new Button("Enable notifications"); WebNotification.onClick(enable).requestPermission();onClickrather than an ordinaryaddClickListener.- Parameters:
component- the component whose clicks trigger the action, notnull- Returns:
- a binding for chaining the action to perform on click
-
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 UIUIDetachedException- if the current UI is not attached to a session
-
permissionSignal
Returns a read-only signal telling whether the browser lets the given UI show notifications. Same semantics aspermissionSignal(); use this overload from background threads or anywhereUI.getCurrent()is unreliable.- Parameters:
ui- the UI to read the signal from, notnull- Returns:
- the read-only permission signal
- Throws:
UIDetachedException- if the UI is not attached to a session
-
show
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 receiveWebNotificationError.Reason.NOT_ALLOWED.- Parameters:
title- the title of the notification, notnull- Returns:
- a handle for reacting to clicks and closing the notification
- Throws:
IllegalStateException- if there is no current UI
-
show
Shows a notification with the given title on the given UI. Same asshow(String); use this overload from background threads or anywhereUI.getCurrent()is unreliable. Like any change to the UI, the call must hold the session lock, so call it insideui.access(...)from a background thread.- Parameters:
title- the title of the notification, notnullui- the UI to show the notification through, notnull- Returns:
- a handle for reacting to clicks and closing the notification
-
show
Shows a notification with the given title on the current UI, with a body text, an icon, a tag or other options. Same asshow(String)otherwise.- Parameters:
title- the title of the notification, notnulloptions- what the notification shows besides its title, and how it behaves, notnull- Returns:
- a handle for reacting to clicks and closing the notification
- Throws:
IllegalStateException- if there is no current UI
-
show
Shows a notification with the given title on the given UI, with a body text, an icon, a tag or other options. Same asshow(String, WebNotificationOptions); use this overload from background threads or anywhereUI.getCurrent()is unreliable. Like any change to the UI, the call must hold the session lock, so call it insideui.access(...)from a background thread.- Parameters:
title- the title of the notification, notnulloptions- what the notification shows besides its title, and how it behaves, notnullui- the UI to show the notification through, notnull- Returns:
- a handle for reacting to clicks and closing the notification
-
closeByTag
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, notnull- Throws:
IllegalStateException- if there is no current UI
-
closeByTag
Closes the notifications with the given tag that the given UI has shown and that are still visible. Same ascloseByTag(String); use this overload from background threads or anywhereUI.getCurrent()is unreliable. Like any change to the UI, the call must hold the session lock, so call it insideui.access(...)from a background thread.- Parameters:
tag- the tag of the notifications to close, notnullui- the UI that showed the notifications, notnull
-