Class NotificationApi

java.lang.Object
com.oorian.html.js.JavaScriptApi
com.oorian.html.js.notification.NotificationApi

public class NotificationApi extends JavaScriptApi
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 Details

    • NotificationApi

      public NotificationApi()
  • Method Details

    • show

      public static void show(Element element, String title)
      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

      public static void show(Element element, String title, String body)
      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

      public static void show(Element element, String title, NotificationOptions options)
      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

      public static void requestPermission(Element element)
      Requests notification permission using the current page context.
      Parameters:
      element - The element that will receive the permission result event.
    • requestPermission

      public static void requestPermission(HtmlPage page, Element element)
      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

      public static void closeAll(HtmlPage page)
      Closes all notifications created by this page.
      Parameters:
      page - The page context.