Repository navigation
8. Show notifications
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 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;-
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. } }
actionsadds buttons under the text, andonActivateruns when the user clicks the notification itself. Both handlers run on a virtual thread, like the handlers of the tray. -
In the
exportNotesbinding, 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();
-
Add the imports:
import dev.ivchenko.lwjwae.notification.Notification; import dev.ivchenko.lwjwae.notification.NotificationAction;
-
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.
| 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
Tutorial
- Set up the project
- Open a window
- Call Java from the page
- Send events from Java to the page
- Draw your own title bar
- Export notes through a native dialog
- Keep the application in the tray
- Show notifications
- Give the application an icon and a name
- Build a native executable
- Package the application
Guides
- Pass objects through the bridge
- Stream bytes between the page and Java
- Open several windows
- Place and control a window
- Make a transparent window
- Add menus
- Register global shortcuts
- Show progress in the taskbar
- Use the clipboard
- Handle dropped files
- Zoom the page
- Set the window icon
- Follow the theme of the desktop
- Use cookies
- Store data in SQLite
- Keep secrets
- Show dialogs
- Run a single instance
- Open your own links and files
- Update the application
- Handle page permissions
- Handle links and page loads
Reference