将应用通知与控制台应用配合使用

应用通知是显示在应用窗口外部的 UI 弹出窗口,向用户提供及时的信息或操作。 通知可以纯粹是信息性的,可以在单击时启动应用,也可以触发后台操作,而无需将应用引入前台。

应用通知的屏幕截图

本文指导你完成从.NET控制台应用创建和发送应用通知的步骤,然后在用户与其交互时处理激活。 本文使用 Windows 应用 SDKMicrosoft.Windows.AppNotifications API。

有关其他框架的应用通知和指南的概述,请参阅 应用通知概述

本文介绍本地通知。 有关从云服务传送通知的信息,请参阅 推送通知

重要

当前不支持具有管理员权限的应用程序的通知。

先决条件

  • 面向 .NET 6 或更高版本的.NET控制台应用
  • Windows 应用 SDK NuGet 包 (Microsoft.WindowsAppSDK

设置您的项目

在项目文件(.csproj),确保TargetFramework包含Windows目标框架:

<TargetFramework>net9.0-windows10.0.19041.0</TargetFramework>

添加 Windows 应用 SDK NuGet 包:

<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.7.250310001" />

对于未打包的应用,请添加:

<WindowsPackageType>None</WindowsPackageType>

注册应用通知

Main 方法中,调用 Register 之前,注册 NotificationInvoked 处理程序。 单击通知时,控制台应用必须保持运行状态才能接收激活回调。

Program.cs

using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;

// Register the notification handler before calling Register
AppNotificationManager.Default.NotificationInvoked += (sender, args) =>
{
    // Handle notification activation.
    // args.Argument contains the arguments from the notification
    // or button that was clicked, as key=value pairs separated
    // by '&', for example "action=acknowledge".
    Console.WriteLine($"Notification activated! Arguments: {args.Argument}");
};

AppNotificationManager.Default.Register();

注释

对于未打包的应用,Register()会自动设置 COM 服务器注册,以便在单击通知时Windows启动应用。 无需手动配置 COM 激活或 AUMID。

发送应用通知

使用 AppNotificationBuilder 构造通知内容,使用 AppNotificationManager.Show 发送通知。

var notification = new AppNotificationBuilder()
    .AddArgument("action", "viewItem")
    .AddText("Console Notification")
    .AddText("This was sent from a console app using Windows App SDK.")
    .AddButton(new AppNotificationButton("Acknowledge")
        .AddArgument("action", "acknowledge"))
    .BuildNotification();

AppNotificationManager.Default.Show(notification);

使应用保持运行

NotificationInvoked要调用处理程序,当用户单击通知时,控制台应用仍必须运行。 如果用户在与通知交互之前退出应用,下一次单击将冷启动一个新进程。

Console.WriteLine("Notification sent! Waiting for activation...");
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();

// Unregister when the app exits
AppNotificationManager.Default.Unregister();

完整示例

下面是一个完整的 Program.cs,用于发送通知并执行激活流程:

using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;

Console.WriteLine("Console App Notification Test");

// Step 1: Register for notification activation
AppNotificationManager.Default.NotificationInvoked += (sender, args) =>
{
    Console.WriteLine($"Notification activated! Arguments: {args.Argument}");
};

AppNotificationManager.Default.Register();

// Step 2: Send a notification
var notification = new AppNotificationBuilder()
    .AddArgument("action", "viewItem")
    .AddText("Console Notification")
    .AddText("This was sent from a console app using Windows App SDK.")
    .AddButton(new AppNotificationButton("Acknowledge")
        .AddArgument("action", "acknowledge"))
    .BuildNotification();

AppNotificationManager.Default.Show(notification);

// Step 3: Wait for user interaction
Console.WriteLine("Notification sent! Click it to test activation.");
Console.WriteLine("Press Enter to exit.");
Console.ReadLine();

AppNotificationManager.Default.Unregister();