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

如何通过Notification Listener获取其他应用Windows Toast通知中的图片

问题:无法通过UserNotificationListener获取ToastImageAndTextXX类型通知中的图片

我基于官方文档开发了一个获取系统通知的应用,目前能通过notif.Notification.Visual.Bindings获取通知绑定内容,但无法提取ToastImageAndText01/02/03/04这类模板通知里的图片资源。

原代码示例

var appName = "whateverApp";

UserNotificationListener listener = UserNotificationListener.Current;

if (!ApiInformation.IsTypePresent("Windows.UI.Notifications.Management.UserNotificationListener") || await AskForNotificationAccess(listener) != UserNotificationListenerAccessStatus.Allowed)
{
    return;
}

IReadOnlyList<UserNotification> notifs = await listener.GetNotificationsAsync(NotificationKinds.Toast);

var notif = notifs.First(x => x.AppInfo.DisplayInfo.DisplayName == appName);

NotificationBinding toastBinding = notif.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric);

if (toastBinding != null)
{
    // 这里只能获取文本,找不到获取通知中图片的方法
    IReadOnlyList<AdaptiveNotificationText> textElements = toastBinding.GetTextElements();
    string titleText = textElements.FirstOrDefault()?.Text;
    string bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text));
}

解决方法

ToastImageAndTextXX属于传统Toast模板,而非通用Toast绑定(ToastGeneric),因此不能通过GetBinding(KnownNotificationBindings.ToastGeneric)获取其内容。需要通过以下步骤提取图片:

  • 遍历所有通知绑定,而非仅获取ToastGeneric绑定
  • 通过NotificationBinding.TemplateName识别传统模板(如"ToastImageAndText01"、"ToastImageAndText02"等)
  • 调用GetAdaptiveContent()获取绑定中的所有元素,从中筛选出AdaptiveNotificationImage类型的元素,即可获取图片源地址

修改后的代码示例

var appName = "whateverApp";

UserNotificationListener listener = UserNotificationListener.Current;

if (!ApiInformation.IsTypePresent("Windows.UI.Notifications.Management.UserNotificationListener") || await AskForNotificationAccess(listener) != UserNotificationListenerAccessStatus.Allowed)
{
    return;
}

IReadOnlyList<UserNotification> notifs = await listener.GetNotificationsAsync(NotificationKinds.Toast);
var targetNotif = notifs.First(x => x.AppInfo.DisplayInfo.DisplayName == appName);

// 遍历所有绑定,寻找传统Toast图片模板
foreach (var binding in targetNotif.Notification.Visual.Bindings)
{
    // 判断是否为ToastImageAndText系列模板
    if (binding.TemplateName.StartsWith("ToastImageAndText"))
    {
        var contentElements = binding.GetAdaptiveContent();
        foreach (var element in contentElements)
        {
            if (element is AdaptiveNotificationImage imageElement)
            {
                // 获取图片源地址
                string imageSource = imageElement.Source;
                // 处理图片资源,如下载或展示
            }
        }
    }
    // 同时保留通用Toast绑定的文本处理逻辑
    else if (binding.Kind == KnownNotificationBindings.ToastGeneric)
    {
        IReadOnlyList<AdaptiveNotificationText> textElements = binding.GetTextElements();
        string titleText = textElements.FirstOrDefault()?.Text;
        string bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text));
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 15:36:25