Nucleus
OS integrationNotifications

Notifications on Windows

Send Windows toast notifications from Kotlin with adaptive text, images, buttons, inputs, headers, and progress bars.

View as Markdown

notification-windows sends Windows 10 and 11 toasts from Kotlin, with adaptive text, images, progress bars, inputs, and headers. It works for MSIX apps and for unpackaged EXE and MSI installs. Add dev.nucleusframework:nucleus.notification-windows.

notification-windowsWindowsGradle plugin: RecommendedWindow runtime: Not neededToasts need the Start menu shortcut carrying the AUMID that the installer creates.

Send a toast

WindowsNotificationCenter.initialize()

WindowsNotificationCenter.show(
    toast {
        visual {
            text("Build finished")
            text("artifact.zip uploaded")
            heroImage("file:///C:/Users/me/hero.png")
        }
        actions {
            button("Open", arguments = "open")
            button("Cancel", arguments = "cancel", activationType = ActivationType.BACKGROUND)
        }
        scenario = ToastScenario.REMINDER
    },
)

WindowsNotificationCenter.addListener(object : ToastNotificationListener {
    override fun onActivated(
        tag: String, group: String, arguments: String, userInputs: Map<String, String>,
    ) { handle(arguments, userInputs) }
    override fun onDismissed(tag: String, group: String, reason: DismissalReason) {}
    override fun onFailed(tag: String, group: String, errorCode: Int) {}
})

Every method does nothing off Windows. arguments carries the launch string or the clicked button's arguments; userInputs maps each input id to its value. Listener callbacks run on the UI thread.

Changed in 3.0

ToastNotificationListener callbacks ran on the AWT event dispatch thread in 2.x. See Migrate from 2.x.

Set the app identity

Windows identifies the sender by its AUMID. MSIX apps use the package identity; unpackaged apps use NucleusApp.aumid, which the Gradle plugin sets. Pass initialize(aumid = "Vendor.App") to override it.

An unpackaged app also needs a Start Menu shortcut with the same AUMID, or toasts can fail to show or vanish from Action Center. The Windows installers create it, so install the app once to see toasts from a dev run. shortcutPolicy sets what initialize does with it:

ShortcutPolicyBehavior
REQUIRE_CREATECreate or update the shortcut. Default for installed apps.
REQUIRE_NO_CREATERequire an existing shortcut and never touch it. Default in dev runs, with a logged warning.
IGNORESkip shortcut handling.

Update a toast in place

Show the toast with a tag, a group, {key} placeholders, and initialData, then push new values:

WindowsNotificationCenter.show(
    toast { visual { progressBar(status = "{status}", value = 0.0) } },
    tag = "upload",
    group = "transfers",
    initialData = ToastNotificationData(sequenceNumber = 1, values = mapOf("status" to "Starting")),
)

WindowsNotificationCenter.update(
    tag = "upload",
    group = "transfers",
    data = ToastNotificationData(sequenceNumber = 2, values = mapOf("status" to "Almost done")),
)

Increase sequenceNumber with each update so Windows drops stale ones. To bind the bar's value, build an AdaptiveProgressBar(valueBind = "progressValue", status = …) and send "progressValue" in values.

Notes

  • Keep one AUMID across the shortcut, your Gradle configuration, and run time. A mismatch produces a toast that shows once and never again.
  • tag and group hold 16 characters at most.
  • Unpackaged apps can load http(s):// images when the server answers fast. For offline use, ship a local file and use file:/// (unpackaged) or ms-appx:/// (MSIX).

API reference

SymbolNotes
toast { visual { } }text, image, appLogo, heroImage, attribution, progressBar, group (columns)
toast { actions { } }textBox, selectionBox, button, contextMenuItem
audio(source, customSource, loop, silent) / silentAudio()Sound
header(id, title, arguments)Group toasts in Action Center
launch, activationType, scenario, duration, displayTimestampToast properties
ToastScenarioDEFAULT, REMINDER, ALARM, INCOMING_CALL, URGENT
ToastDurationDEFAULT, SHORT, LONG
DismissalReasonUSER_CANCELED, APPLICATION_HIDDEN, TIMED_OUT
showSimple(title, body, body2) / showFromXmlText-only toast / raw toast XML
getHistory { entries, error -> }Action Center entries
remove(tag, group) / removeGroup(group) / clearAll()Remove toasts
removeListener(listener) / uninitialize()Remove a listener / release native resources at shutdown

What's next

Was this page helpful?
Edit on GitHub

Last updated on