获取Windows 10 UWP中的通知来源?

时间:2019-03-31 23:38:56

标签: c# uwp notifications windows-10

我正在编写带有通知侦听器的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是通知来自的应用程序的名称,但它似乎为空或无法正常工作。从这样的通知中准确地说: notification example

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

1 个答案:

答案 0 :(得分:0)

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

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

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

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