Skip to content

8. Show notifications

fakeivchenko edited this page Sep 28, 2026 · 2 revisions

A notification tells the user about something that happened while they looked elsewhere, such as an export that finished while the window was hidden in the tray. In this step, the application shows a notification after the export, with a button that brings the window back.

The notification of an export on KDE Plasma and on Windows 11

Name the application

The desktop shows the name of the application on each notification. Application.launch starts an application without a name, so Windows falls back to the name of the main class, or of the JAR file that runs it. To give the application a name, create it yourself instead of calling launch.

In Main.java, replace the body of main:

public static void main(String[] args) {
  ApplicationParameters parameters = ApplicationParameters.builder().name("Notes").build();
  try (Application application = Application.create(parameters)) {
    Window window =
        application.open(
            WindowParameters.builder()
                .title("Notes")
                .size(720, 520)
                .minimumSize(480, 360)
                .decorated(false)
                .closeAction(CloseAction.HIDE)
                .build());
    setUp(window);
    window.load("app/index.html");
    window.show();
    application.run();
  }
}

These are the steps that launch takes for you. setUp runs before load, so the page finds its bindings from its first line of script. run blocks until the application quits, and the try-with-resources block closes it afterward.

The name does more than label notifications. It goes into the user agent of the window, and it names the directory where the application keeps its data, such as the size and the place of a window that has a stateKey.

Add the import:

import dev.ivchenko.lwjwae.ApplicationParameters;

Show the notification

  1. In Main.java, add a method that shows the notification:

    static void notifyExported(Window window, Path file) {
      try {
        window
            .application()
            .showNotification(
                Notification.builder()
                    .title("Notes exported")
                    .body(NOTES.size() + " notes in " + file.getFileName())
                    .icon("app/tray.png")
                    .actions(new NotificationAction("Show notes", window::focus))
                    .onActivate(window::focus)
                    .build());
      } catch (UnsupportedOperationException e) {
        // No notifications on this desktop: the status line on the page says the same.
      }
    }

    actions adds buttons under the text, and onActivate runs when the user clicks the notification itself. Both handlers run on a virtual thread, like the handlers of the tray.

  2. In the exportNotes binding, call it after the file is written:

          try {
            Files.write(file.get(), NOTES);
          } catch (IOException e) {
            return "Export failed: " + e.getMessage();
          }
          notifyExported(window, file.get());
          return "Exported to " + file.get();
  3. Add the imports:

    import dev.ivchenko.lwjwae.notification.Notification;
    import dev.ivchenko.lwjwae.notification.NotificationAction;
  4. Run the application, save a few notes, and click Export. After you pick a file, the notification appears. Click Show notes to bring the window to the front, even after you closed it to the tray.

How each platform shows it

Platform Notification
Linux The notification server of the desktop shows it, through org.freedesktop.Notifications. Most servers show the image and the buttons.
Windows A toast. The library registers the name of the application for the current user, and a toast that times out moves to Notification Center, where the user can still click it.
macOS Only for an application packaged as an .app bundle with an identifier. Under the java launcher, showNotification throws UnsupportedOperationException, which the catch block handles. The first notification asks the user for permission.

A notification belongs to the application: it stays when the window is hidden or closed, and it goes away when the application quits, because its buttons go with it. To take a notification back earlier, keep the NotificationHandle that showNotification returns and call close on it.


Previous: Keep the application in the tray · Next: Give the application an icon and a name

Clone this wiki locally