Send a notification from a tray menu. Linux needs a compatible tray and notification service.
dotnet add package RecursiveAI.OneAppRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
// app: the running OneApp AppHost await app.Shell.ShowTrayAsync("Export", "dialog-information", [ new() { Label = "Send notification", Click = async () => await app.Shell.NotifyAsync( new("Export complete")) } ]);
The example is the full program below: unzip it and dotnet run.
Full program
Open the Tray notification icon’s menu, then choose Send notification. The app asks the desktop to display Export complete, with the text Your report is ready. The menu also has Quit.
The OnePlatform.DBus and OnePlatform.Shell namespaces come with RecursiveAI.OneApp; no separate install is needed.
using OneApp; using OnePlatform.DBus; using OnePlatform.Shell; using var app = AppHost.Create(new("<h1>Tray notification</h1>") { AppId = "org.example.TrayNotification", Title = "Tray notification", Width = 400, Height = 180 }); app.Post(async () => { try { await app.Shell.ShowTrayAsync("Tray notification", "dialog-information", [ new TrayMenuItem { Label = "Send notification", Click = Notify }, new TrayMenuItem { Label = "Quit", Click = () => app.Quit() } ]); Console.WriteLine("Tray item ready."); } catch (Exception error) when (error is DBusServiceUnavailableException or ShellUnavailableException) { Console.Error.WriteLine(error.Message); Environment.ExitCode = 1; app.Quit(); } }); app.Run(); async void Notify() { try { await app.Shell.NotifyAsync(new AppNotification("Export complete") { Body = "Your report is ready.", Id = "export" }); Console.WriteLine("Desktop accepted the notification."); } catch (Exception error) when (error is DBusServiceUnavailableException or ShellUnavailableException) { Console.Error.WriteLine(error.Message); } }
Tray item ready. Desktop accepted the notification.
The second line means the service accepted the request, not that the user saw a banner. If Linux has no compatible tray or notification service, the corresponding call can throw DBusServiceUnavailableException; the full program reports the error.
Variations
Replace a notification. Pick Send notification twice. Sending another notification with the same Id requests a replacement.
Keep only the tray icon. AppHost.Create opens a window, but the tray doesn’t need it to stay open: set Exit = ExitPolicy.Never in the app options, then close the window after the tray item appears and use the tray’s Quit item to exit.
Related recipes
- Build a desktop app with HTML and CSS in C#
- Automate UI tests for a C# desktop app
- Open a native file dialog in a C# desktop app
The short answer
Use app.Shell.ShowTrayAsync to register a tray item with a menu, then call app.Shell.NotifyAsync from a menu callback. The tray item is the persistent entry where the user starts the action. The notification is a separate message that the operating system can show outside your window. Keep the app’s event loop running so it can receive the menu click and send the notification.
Where is the tray icon on a PC?
Look in the desktop’s status area, not inside the app window. The desktop provides the tray and decides which icons it shows and which it puts in an overflow menu. On Linux the app registers a StatusNotifierItem with a StatusNotifierWatcher, such as the one provided by Plasma. If there’s no watcher, the sample won’t create an icon. On macOS the equivalent entry is a menu-bar status item; on Windows the app uses the system tray.
The returned TrayHandle is available as app.Shell.Tray, and the host keeps it even when your setup code doesn’t store the return value. Use the handle to replace the menu or icon while the app is running, or dispose it to remove the entry. On Linux, use a name your icon theme provides or give the app its own icon.
Why am I not receiving desktop notifications?
First distinguish registration from delivery. If the tray menu opens but nothing happens when you choose Send notification, check the terminal for a notification-service error. On Linux OneApp can use the notification portal or fall back to the desktop notification service. Seeing a tray icon confirms only the tray side; the two services are independent.
On Windows a toast only appears when the app is not in the foreground and notifications are allowed; a basic toast doesn’t require packaging, but buttons on it do.
If the service accepted the message, check the desktop’s notification settings. The success message in the terminal doesn’t tell you whether the desktop displayed a banner. Also check how the app is packaged: on macOS the native notification API needs an app bundle, and the user must grant notification permission. The desktop shell support table describes which services the calls use.