从 windows 个通用应用程序发送 Toast 通知

Send a Toast Notification from windows universal Application

我正在尝试找到一种从 windows 通用应用程序发送 toast 通知的方法。 Azure 文档确实提到了一种从控制台应用程序执行此操作的方法。这不符合我的用例,我想从 windows 通用应用程序发送 toast 通知。问题是 nugget 包 Microsoft.Azure.Service.Bus 与 windows 商店或 windows phone 8.1 application.

不兼容

是否有任何 Windows Azure 库允许我从 Windows 通用应用程序发送 toast notification

通常您不会从 Universal 应用发送消息框。您可以将 toast 发送到应用程序,让用户知道服务器上发生了一些事情。

如果您想安排从应用程序到本地系统的 toast,则不需要 Azure(或其他 Web 服务)。您可以使用预定通知直接安排它。参见 Quickstart: Sending a toast notification (XAML) and How to schedule a toast notification (XAML)

如果您想在其他地方推送通知,那么您可以设置一个 Azure 通知中心并通过其 REST service 通过 HttpClient class.

连接到它

更常见的是,您的应用会联系您的后端服务器以连接到通知中心。这可以通过 Azure 移动服务轻松设置。参见 Add push notifications to your Mobile Services app

Toast notifications are small pop ups that appear on the screen for few seconds. They convey messages and can be customized to even play different sounds.

查看 Windows Universal Apps notifications sample 了解更多信息。

以下两个基本示例:

Sample 1 text only

const ToastTemplateType toastTemplate = ToastTemplateType.ToastText01;
var toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);

var toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode("Hello World!"));

var toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);

Sample 2 text and image

const ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText01;
var toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);

var toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode("Hello World!"));

var toastImageAttributes = toastXml.GetElementsByTagName("image");
((XmlElement)toastImageAttributes[0]).SetAttribute("src", "https://i.stack.imgur.com/wzdIt.jpg");

var toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);