You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#版WinUI 3打包应用中实现推送通知唤醒功能

WinUI 3 C#打包应用推送通知实现指南

一、核心逻辑:无后台任务的唤醒机制

WinUI3打包式桌面应用依赖Windows App SDK的推送通知管道,当应用处于终止状态时,系统会直接唤醒应用进程(而非后台任务),并通过应用激活事件传递原始通知内容,无需单独注册UWP式的后台任务。

二、C#版实现步骤

1. 配置打包应用权限

确保你的WinUI3应用为MSIX打包模式,在打包项目的Package.appxmanifest中完成以下配置:

  • 在「功能」标签页勾选「推送通知」权限。
  • 确认Package Identity中的发布者信息与Azure通知中心注册的内容一致。

2. 注册推送通知通道

在应用启动阶段(如App.xaml.cs的OnLaunched方法),获取并注册推送通道:

using Windows.Networking.PushNotifications;

private async Task RegisterPushChannelAsync()
{
    var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
    // 将channel.Uri发送至后端服务,用于后续推送调用
}

3. 处理终止状态下的激活唤醒

在App.xaml.cs中重写OnActivated方法,捕获推送通知激活事件:

protected override void OnActivated(IActivatedEventArgs args)
{
    base.OnActivated(args);

    // 处理Toast通知激活(含原始通知内容)
    if (args is ToastNotificationActivatedEventArgs toastArgs)
    {
        string rawContent = toastArgs.Argument;
        ShowToastNotification(rawContent);
    }
    // 处理后台推送激活(应用在后台时)
    else if (args is PushNotificationReceivedEventArgs pushArgs)
    {
        if (pushArgs.NotificationType == PushNotificationType.Raw)
        {
            ShowToastNotification(pushArgs.RawNotification.Content);
            pushArgs.Cancel = true; // 阻止系统默认行为
        }
    }

    // 确保主窗口正常激活
    if (Window.Current == null)
    {
        var mainWindow = new MainWindow();
        mainWindow.Activate();
    }
    else
    {
        Window.Current.Activate();
    }
}

4. 封装Toast通知显示方法

推荐使用Windows Community Toolkit简化Toast创建:

using Windows.UI.Notifications;
using Microsoft.Toolkit.Uwp.Notifications;

private void ShowToastNotification(string rawContent)
{
    var toastContent = new ToastContent()
    {
        Visual = new ToastVisual()
        {
            BindingGeneric = new ToastBindingGeneric()
            {
                Children =
                {
                    new AdaptiveText { Text = "新通知" },
                    new AdaptiveText { Text = rawContent }
                }
            }
        },
        Launch = rawContent // 点击Toast时传递的参数
    };

    var toast = new ToastNotification(toastContent.GetXml());
    ToastNotificationManager.CreateToastNotifier().Show(toast);
}

5. 处理应用运行时的推送通知

注册PushNotificationReceived事件,处理前台运行时的原始通知:

private void RegisterPushEvent()
{
    PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync().ContinueWith(task =>
    {
        var channel = task.Result;
        channel.PushNotificationReceived += OnPushNotificationReceived;
    });
}

private void OnPushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs args)
{
    if (args.NotificationType == PushNotificationType.Raw)
    {
        ShowToastNotification(args.RawNotification.Content);
        args.Cancel = true;
    }
}

三、关键注意事项

  • 必须MSIX打包:未打包的WinUI3桌面应用无法接入Windows推送通知管道。
  • 依赖包一致性:确保应用包签名与Azure通知中心的配置匹配,否则无法接收推送。
  • 激活优先级:终止状态下唤醒时,OnActivated会先于OnLaunched执行,需确保激活逻辑能初始化必要组件。

内容的提问来源于stack exchange,提问作者Sumnoon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 23:52:42