.NET MAUI Windows应用系统托盘通知无法弹出的问题求助
Windows端系统托盘/Toast通知无法弹出的解决方案
针对你遇到的WinUI应用通知无法弹出问题,以下是几个可落地的排查和解决步骤:
1. 检查系统通知权限与设置
- 打开Windows设置 > 系统 > 通知,找到你的应用,确认允许通知开关处于开启状态,同时确保“横幅”“操作中心”等通知展示选项未被禁用。
- 检查是否开启了专注助手或“请勿打扰”模式,这类功能会屏蔽所有通知。
2. 修正Toast通知的配置问题
你的代码中可能存在两个潜在问题:
- 图标路径有效性:确认
ms-appx:///Assets/dotnet_bot.png路径正确,图片存在且格式为PNG/JPG(建议尺寸≥36x36)。路径错误会导致Toast静默失效。 - 激活参数错误:
AddToastActivationInfo(null, ToastActivationType.Foreground)中传入null不符合Foreground激活的要求,可改为:- 若无需唤起应用前台,改用
ToastActivationType.Background; - 若需要唤起,传入有效的页面路由参数(如
"/MainPage"); - 或直接省略该方法,使用默认激活行为。
- 若无需唤起应用前台,改用
修正后的示例代码:
public void ShowNotification(string title, string body) { new ToastContentBuilder() .AddAppLogoOverride(new Uri("ms-appx:///Assets/dotnet_bot.png")) .AddText(title, hintStyle: AdaptiveTextStyle.Header) .AddText(body, hintStyle: AdaptiveTextStyle.Body) .Show(); }
3. 确保WinUI桌面应用的Toast激活配置
WinUI 3桌面应用需要额外配置Toast激活器才能正常触发通知:
- 创建激活器类:
using Microsoft.Toolkit.Uwp.Notifications; public class ToastNotificationActivator : ToastNotificationActivator { public override void OnActivated(ToastNotificationActivatedEventArgsCompat e) { // 处理通知激活逻辑 } }
- 在
Package.appxmanifest的Extensions节点添加配置(替换为你的激活器GUID):
<Extensions> <uap:Extension Category="windows.toastNotificationActivation"> <uap:ToastNotificationActivation ToastActivatorCLSID="{YOUR-GUID-HERE}" /> </uap:Extension> </Extensions>
- 在应用启动时注册激活器:
var activator = new ToastNotificationActivator(); activator.Register();
4. 改用系统托盘气泡通知(若目标是任务栏通知)
如果你的需求是系统托盘(任务栏右下角)的气泡通知,而非Toast通知,可以使用第三方库实现:
using Hardcodet.Wpf.TaskbarNotification; // 初始化系统托盘图标(需在应用启动时执行) var notifyIcon = new TaskbarIcon(); notifyIcon.Icon = new System.Drawing.Icon("Assets/AppIcon.ico"); notifyIcon.ToolTipText = "你的应用名称"; // 显示气泡通知 notifyIcon.ShowBalloonTip(title, body, BalloonIcon.Info);
注意:需确保应用始终保持系统托盘图标实例存在,否则气泡通知无法弹出。
5. 调试通知失败原因
- 监听Toast的
Failed事件捕获错误:
var toastContent = new ToastContentBuilder() .AddText(title) .AddText(body) .GetToastContent(); var toast = new ToastNotification(toastContent.GetXml()); toast.Failed += (s, e) => { Debug.WriteLine($"通知失败错误码:{e.ErrorCode}"); }; ToastNotificationManager.CreateToastNotifier().Show(toast);
- 打开Windows事件查看器,查看
应用程序和服务日志 > Microsoft > Windows > ToastNotifications下的日志,获取详细失败信息。
内容的提问来源于stack exchange,提问作者rebe21
相关产品推荐
相关产品推荐

