Nucleus
OS integrationNotifications

Notifications on Linux

Send freedesktop desktop notifications over D-Bus from Kotlin, with urgency, hints, sounds, images, and action callbacks.

View as Markdown

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-linuxLinuxGradle plugin: Not neededWindow runtime: Not needed

Send 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; set hints.imagePath for a consistent icon. getServerInformation() and getCapabilities() tell you which server runs and what it supports.
  • Most servers show Urgency.CRITICAL notifications through Do Not Disturb and never expire them.
  • Under Xvfb or on a headless CI runner, notify returns 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's onClosed and onDismissed never fire), no expiry timeout, no sound and no raw imageData; hints.imagePath still works. Actions and the click on the notification arrive as usual.

API reference

SymbolNotes
LinuxNotificationCenter.isAvailablefalse off Linux or when the native library did not load
UrgencyLOW, NORMAL, CRITICAL
NotificationHintsAll 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.
NotificationSoundTheme names in Alert, Notification, Action, InputFeedback, Game; Custom("x-myapp-…") for others
CloseReasonEXPIRED, DISMISSED, CLOSED, UNDEFINED
closeNotification(id) / removeListener(listener)Close a notification / remove a listener

What's next

Was this page helpful?
Edit on GitHub

Last updated on