Package com.oorian.html.js.notification
Class NotificationApi
java.lang.Object
com.oorian.html.js.JavaScriptApi
com.oorian.html.js.notification.NotificationApi
Provides access to the browser's Notifications API from server-side Java code.
This class wraps the browser's Notification API, enabling server-side code to display desktop notifications to the user. Notifications can include titles, body text, icons, and other options.
Usage:
public class NotifyPage extends HtmlPage implements NotificationListener {
@Override
protected void createBody(Body body) {
Button notifyBtn = new Button("Notify");
notifyBtn.registerListener(this, NotificationClickEvent.class);
notifyBtn.registerListener(this, NotificationCloseEvent.class);
notifyBtn.registerListener((MouseClickedEvent e) -> {
NotificationApi.show(notifyBtn, "Hello!", "This is a notification");
}, MouseClickedEvent.class);
body.addElement(notifyBtn);
}
@Override
public void onEvent(NotificationClickEvent event) {
// User clicked the notification
}
@Override
public void onEvent(NotificationCloseEvent event) {
// Notification was closed
}
@Override
public void onEvent(NotificationErrorEvent event) {
// Error showing notification
}
}
Browser Support:
The Notification API requires user permission. Browsers will prompt the user for permission before showing notifications.
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcloseAll()Closes all notifications created by this page using the current page context.static voidCloses all notifications created by this page.static voidrequestPermission(Element element) Requests notification permission using the current page context.static voidrequestPermission(HtmlPage page, Element element) Requests notification permission.static voidShows a notification with the specified title using the current page context.static voidshow(Element element, String title, NotificationOptions options) Shows a notification with the specified options using the current page context.static voidShows a notification with the specified title and body using the current page context.static voidShows a notification with the specified title, body, and options.
-
Constructor Details
-
NotificationApi
public NotificationApi()
-
-
Method Details
-
show
Shows a notification with the specified title using the current page context.- Parameters:
element- The element that will receive notification events.title- The notification title.
-
show
Shows a notification with the specified title and body using the current page context.- Parameters:
element- The element that will receive notification events.title- The notification title.body- The notification body text.
-
show
Shows a notification with the specified options using the current page context.- Parameters:
element- The element that will receive notification events.title- The notification title.options- Configuration options for the notification.
-
show
public static void show(HtmlPage page, Element element, String title, String body, NotificationOptions options) Shows a notification with the specified title, body, and options.- Parameters:
page- The page context.element- The element that will receive notification events.title- The notification title.body- The notification body text.options- Configuration options for the notification, or null for defaults.
-
requestPermission
Requests notification permission using the current page context.- Parameters:
element- The element that will receive the permission result event.
-
requestPermission
Requests notification permission.- Parameters:
page- The page context.element- The element that will receive the permission result event.
-
closeAll
public static void closeAll()Closes all notifications created by this page using the current page context. -
closeAll
Closes all notifications created by this page.- Parameters:
page- The page context.
-