|
| 1 | +--- |
| 2 | +title: Send a local app notification from a console app |
| 3 | +description: Learn how to send a local app notification from a .NET console app and handle the user clicking the notification using the Windows App SDK. |
| 4 | +ms.date: 04/08/2026 |
| 5 | +ms.topic: how-to |
| 6 | +keywords: windows 11, windows 10, windows app sdk, winappsdk, console, send app notifications, notifications, toast notifications, how to, quickstart, c#, csharp |
| 7 | +ms.localizationpriority: medium |
| 8 | +--- |
| 9 | + |
| 10 | +# Send a local app notification from a console app |
| 11 | + |
| 12 | +An app notification is a message that your app can construct and deliver to your user while they are not currently inside your app. |
| 13 | + |
| 14 | +:::image type="content" source="images/toast-notification.png" alt-text="Screenshot of an app notification" width="628"::: |
| 15 | + |
| 16 | +This article walks you through the steps to send and handle app notifications from a .NET console app using the [Windows App SDK](/windows/apps/windows-app-sdk/). The Windows App SDK `Microsoft.Windows.AppNotifications` APIs handle all the complexity of notification registration and activation for both packaged and unpackaged apps. |
| 17 | + |
| 18 | +> [!NOTE] |
| 19 | +> For WinUI apps, see [Quickstart: App notifications in the Windows App SDK](app-notifications-quickstart.md). For other app types, see [WPF](send-local-toast-wpf.md), [WinForms](send-local-toast-winforms.md), or [UWP](send-local-toast-uwp.md). |
| 20 | +
|
| 21 | +> [!IMPORTANT] |
| 22 | +> Notifications for elevated (admin) apps are not currently supported. |
| 23 | +
|
| 24 | +## Prerequisites |
| 25 | + |
| 26 | +- A .NET console app targeting .NET 6 or later |
| 27 | +- The [Windows App SDK](/windows/apps/windows-app-sdk/) NuGet package (`Microsoft.WindowsAppSDK`) |
| 28 | + |
| 29 | +### Set up your project |
| 30 | + |
| 31 | +In your project file (`.csproj`), make sure the `TargetFramework` includes a Windows target framework: |
| 32 | + |
| 33 | +```xml |
| 34 | +<TargetFramework>net9.0-windows10.0.19041.0</TargetFramework> |
| 35 | +``` |
| 36 | + |
| 37 | +Add the Windows App SDK NuGet package: |
| 38 | + |
| 39 | +```xml |
| 40 | +<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250310001" /> |
| 41 | +``` |
| 42 | + |
| 43 | +For unpackaged apps, add: |
| 44 | + |
| 45 | +```xml |
| 46 | +<WindowsPackageType>None</WindowsPackageType> |
| 47 | +``` |
| 48 | + |
| 49 | +## Step 1: Register for app notifications |
| 50 | + |
| 51 | +In your `Main` method, register the `NotificationInvoked` handler *before* calling `Register()`. The console app must remain running to receive activation callbacks when notifications are clicked. |
| 52 | + |
| 53 | +**Program.cs** |
| 54 | + |
| 55 | +```csharp |
| 56 | +using Microsoft.Windows.AppNotifications; |
| 57 | +using Microsoft.Windows.AppNotifications.Builder; |
| 58 | + |
| 59 | +// Register the notification handler before calling Register |
| 60 | +AppNotificationManager.Default.NotificationInvoked += (sender, args) => |
| 61 | +{ |
| 62 | + // Handle notification activation. |
| 63 | + // args.Argument contains the arguments from the notification |
| 64 | + // or button that was clicked, as key=value pairs separated |
| 65 | + // by '&', for example "action=acknowledge". |
| 66 | + Console.WriteLine($"Notification activated! Arguments: {args.Argument}"); |
| 67 | +}; |
| 68 | + |
| 69 | +AppNotificationManager.Default.Register(); |
| 70 | +``` |
| 71 | + |
| 72 | +> [!NOTE] |
| 73 | +> For unpackaged apps, `Register()` automatically sets up the COM server registration that allows Windows to launch your app when a notification is clicked. You don't need to configure COM activation or an AUMID manually. |
| 74 | +
|
| 75 | +## Step 2: Send an app notification |
| 76 | + |
| 77 | +Use the `AppNotificationBuilder` API to construct and send a notification. |
| 78 | + |
| 79 | +```csharp |
| 80 | +var notification = new AppNotificationBuilder() |
| 81 | + .AddArgument("action", "viewItem") |
| 82 | + .AddText("Console Notification") |
| 83 | + .AddText("This was sent from a console app using Windows App SDK.") |
| 84 | + .AddButton(new AppNotificationButton("Acknowledge") |
| 85 | + .AddArgument("action", "acknowledge")) |
| 86 | + .BuildNotification(); |
| 87 | + |
| 88 | +AppNotificationManager.Default.Show(notification); |
| 89 | +``` |
| 90 | + |
| 91 | +## Step 3: Keep the app running |
| 92 | + |
| 93 | +For the `NotificationInvoked` handler to be called, the console app must still be running when the user clicks the notification. If the app exits before the user interacts with the notification, the next click will cold-launch a new process. |
| 94 | + |
| 95 | +```csharp |
| 96 | +Console.WriteLine("Notification sent! Waiting for activation..."); |
| 97 | +Console.WriteLine("Press Enter to exit."); |
| 98 | +Console.ReadLine(); |
| 99 | + |
| 100 | +// Unregister when the app exits |
| 101 | +AppNotificationManager.Default.Unregister(); |
| 102 | +``` |
| 103 | + |
| 104 | +## Complete example |
| 105 | + |
| 106 | +Here's a complete `Program.cs` that sends a notification and handles activation: |
| 107 | + |
| 108 | +```csharp |
| 109 | +using Microsoft.Windows.AppNotifications; |
| 110 | +using Microsoft.Windows.AppNotifications.Builder; |
| 111 | + |
| 112 | +Console.WriteLine("Console App Notification Test"); |
| 113 | + |
| 114 | +// Step 1: Register for notification activation |
| 115 | +AppNotificationManager.Default.NotificationInvoked += (sender, args) => |
| 116 | +{ |
| 117 | + Console.WriteLine($"Notification activated! Arguments: {args.Argument}"); |
| 118 | +}; |
| 119 | + |
| 120 | +AppNotificationManager.Default.Register(); |
| 121 | + |
| 122 | +// Step 2: Send a notification |
| 123 | +var notification = new AppNotificationBuilder() |
| 124 | + .AddArgument("action", "viewItem") |
| 125 | + .AddText("Console Notification") |
| 126 | + .AddText("This was sent from a console app using Windows App SDK.") |
| 127 | + .AddButton(new AppNotificationButton("Acknowledge") |
| 128 | + .AddArgument("action", "acknowledge")) |
| 129 | + .BuildNotification(); |
| 130 | + |
| 131 | +AppNotificationManager.Default.Show(notification); |
| 132 | + |
| 133 | +// Step 3: Wait for user interaction |
| 134 | +Console.WriteLine("Notification sent! Click it to test activation."); |
| 135 | +Console.WriteLine("Press Enter to exit."); |
| 136 | +Console.ReadLine(); |
| 137 | + |
| 138 | +AppNotificationManager.Default.Unregister(); |
| 139 | +``` |
| 140 | + |
| 141 | +## Related content |
| 142 | + |
| 143 | +- [Quickstart: App notifications in the Windows App SDK](app-notifications-quickstart.md) |
| 144 | +- [App notification content](adaptive-interactive-toasts.md) |
| 145 | +- [AppNotificationManager Class](/windows/windows-app-sdk/api/winrt/microsoft.windows.appnotifications.appnotificationmanager) |
| 146 | +- [AppNotificationBuilder Class](/windows/windows-app-sdk/api/winrt/microsoft.windows.appnotifications.builder.appnotificationbuilder) |
0 commit comments