首页 > 解决方案 > 在 Windows 10 uwp 中获取通知来源?

问题描述

我正在编写一个带有通知侦听器的 uwp 应用程序,并且我正在尝试获取通知的来源(例如 Google Chrome 和它来自的网站)。

我尝试将 AppInfo.DisplayInfo 用于 UserNotification 但我无法让它打印信息,而且我不确定这是否是正确的方法。

IReadOnlyList<UserNotification> notifs = await MainPage.listener.GetNotificationsAsync(Windows.UI.Notifications.NotificationKinds.Toast);
UserNotification n = notifs.Last();
var name = n.AppInfo.DisplayInfo.DisplayName;

我希望 name 是通知来自的应用程序的名称,但它似乎是空的或只是不工作。从这样的通知中准确地说: 通知示例

我想提取“Google Chrome”和/或“www.reddit.com”。

标签: c#uwpnotificationswindows-10

解决方案


希望您现在已经找到了解决方案,但万一这对任何人都有帮助:

通知不知道您的消息来自浏览器。整个 Windows 通知系统都没有考虑到这一点。Windows从应用程序接收UserNotification,在您的情况下恰好是浏览器。所以你的“起源”是应用程序“谷歌浏览器”。你最好的办法是尝试从通知本身内部获取链接,如果它在体内某处。

我真的认为您应该能够在通知文本中找到链接,但我需要更多关于您收到的确切信息的信息才能确定。如果您想知道如何阅读通知的文本,请执行以下操作:

string appName = notif.AppInfo.DisplayInfo.DisplayName; //this will get you "Google Chrome"
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));
    string website = ParseWebsiteFromText(bodyText); //my guess is the info you want is in here
}

如果可用,您必须从那里解析所需的信息。

如果您想了解更多信息,我建议您从此处阅读文档:https ://docs.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/notification-listener 。


推荐阅读