c# - 在 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”。
解决方案
希望您现在已经找到了解决方案,但万一这对任何人都有帮助:
通知不知道您的消息来自浏览器。整个 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 。
推荐阅读
- database - mongo 查询 - 从对象数组中获取特定对象(它的 `_id` 是已知的)但是这个数组也是文档列表的一部分
- javascript - 承诺一个 redux 表单 onSubmit 函数
- ruby-on-rails - has_many 关联状态检查执行 N+1 查询活动管理员
- angular - 我们可以从 Angular 4 中的本地路径读取文件吗?
- apache-flink - 如何高效地计算来自一个来源的多 flink 流结果
- wordpress - WordPress 自动重定向到登录页面
- php - 在 Laravel 中使用通过 composer 安装的包
- graphql - 是否可以使用 VTL 随机化 DynamoDB 查询结果?
- asp.net-mvc - 从 __MigrationHistory 表中删除数据后的 EF 6 Code First 迁移
- html - 当我以角度 5 重复表单数组时,下拉项目被填充