Notifications on Linux
Send freedesktop desktop notifications over D-Bus from Kotlin, with urgency, hints, sounds, images, and action callbacks.
notification-linux sends freedesktop desktop notifications on GNOME, KDE, Cinnamon, Xfce, and
other desktops. Add dev.nucleusframework:nucleus.notification-linux; it brings the typed icon
names of freedesktop-icons.
notification-linuxGradle plugin: Not neededWindow runtime: Not neededSend a notification
val id: Int = LinuxNotificationCenter.notify(
Notification(
appName = "MyApp",
summary = "Build finished",
body = "myapp.zip is ready",
appIcon = FreedesktopIcon.Status.DIALOG_INFORMATION,
hints = NotificationHints(
urgency = Urgency.NORMAL,
soundName = NotificationSound.Notification.DIALOG_INFORMATION,
),
actions = listOf(
NotificationAction(NotificationAction.DEFAULT_KEY, "Open"), // body click
NotificationAction("archive", "Archive"),
),
),
)notify returns the server-assigned ID, or 0 on failure. Pass it as replacesId to replace a
notification in place, or to closeNotification(id). expireTimeout is in milliseconds: -1
(the default) lets the server decide, 0 keeps the notification until the user closes it.
Receive callbacks
LinuxNotificationCenter.addListener(object : LinuxNotificationListener {
override fun onActionInvoked(notificationId: Int, actionKey: String) { /* … */ }
override fun onClosed(notificationId: Int, reason: CloseReason) { /* … */ }
override fun onActivationToken(notificationId: Int, token: String) { /* … */ }
})All three methods have empty defaults. Callbacks run on the UI thread.
To bring a window to the front from onActionInvoked, call requestFocus() on it. The window
takes the activation token the server sent with the click, which Wayland requires.
onActivationToken delivers that token for an app that hands it on itself.
Changed in 3.0
Listener callbacks ran on the AWT event dispatch thread in 2.x. See Migrate from 2.x.
Notes
- Servers render the specification differently. GNOME Shell often ignores
appIcon; sethints.imagePathfor a consistent icon.getServerInformation()andgetCapabilities()tell you which server runs and what it supports. - Most servers show
Urgency.CRITICALnotifications through Do Not Disturb and never expire them. - Under Xvfb or on a headless CI runner,
notifyreturns without error but nothing shows. - In a Flatpak, Nucleus sends the notifications through the notification portal: the manifest
needs no permission, and Flathub refuses
--talk-name=org.freedesktop.Notifications. The portal has no close event (the listener'sonClosedandonDismissednever fire), no expiry timeout, no sound and no rawimageData;hints.imagePathstill works. Actions and the click on the notification arrive as usual.
API reference
| Symbol | Notes |
|---|---|
LinuxNotificationCenter.isAvailable | false off Linux or when the native library did not load |
Urgency | LOW, NORMAL, CRITICAL |
NotificationHints | All optional: urgency, category, desktopEntry, imageData, imagePath, actionIcons, soundFile, soundName, suppressSound, resident, transient, x, y |
ImageData(width, height, rowstride, hasAlpha, data) | Raw RGB or RGBA pixels. channels defaults to 4 with alpha, 3 without; bitsPerSample to 8. |
NotificationSound | Theme names in Alert, Notification, Action, InputFeedback, Game; Custom("x-myapp-…") for others |
CloseReason | EXPIRED, DISMISSED, CLOSED, UNDEFINED |
closeNotification(id) / removeListener(listener) | Close a notification / remove a listener |
What's next
- FreeDesktop icon names: typed names for
appIconandimagePath. - Cross-platform notifications: the shared notification API.
- Linux launcher: badges and quicklists on the Linux dock.
Last updated on