private async Task SendTileNotification(NotifyMessage notify) { var tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWide310x150ImageAndText01); UpdateTileParts(notify, tileXml); var tileNotification = new TileNotification(tileXml) { Tag = notify.Tag ?? notify.Id.ToString() }; TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification); tileNotification.ExpirationTime = DateTimeOffset.UtcNow.AddMinutes(10); await this.dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { try { TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification); } catch (Exception) { // already updated is ok. } }); }
private static void UpdateTileParts(NotifyMessage notify, XmlDocument tileXml) { var squareTileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquare150x150Text01); var squareTileTextAttributes = squareTileXml.GetElementsByTagName("text"); squareTileTextAttributes[0].AppendChild(squareTileXml.CreateTextNode(notify.Message)); var node = tileXml.ImportNode(squareTileXml.GetElementsByTagName("binding").Item(0), true); tileXml.GetElementsByTagName("visual").Item(0).AppendChild(node); if (!string.IsNullOrWhiteSpace(notify.Image)) { var img = tileXml.GetElementsByTagName("image"); ((XmlElement)img[0]).SetAttribute("src", notify.Image); ((XmlElement)img[0]).SetAttribute("alt", string.Empty); } }
private async Task SendToastNotification(NotifyMessage notify) { var toastTemplate = ToastTemplateType.ToastImageAndText01; var toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate); var toastTextElements = toastXml.GetElementsByTagName("text"); toastTextElements[0].AppendChild(toastXml.CreateTextNode(notify.Message)); if (!string.IsNullOrWhiteSpace(notify.Image)) { var toastImageAttributes = toastXml.GetElementsByTagName("image"); ((XmlElement)toastImageAttributes[0]).SetAttribute("src", notify.Image); ((XmlElement)toastImageAttributes[0]).SetAttribute("alt", string.Empty); } var toastNode = toastXml.SelectSingleNode("/toast"); var audio = toastXml.CreateElement("audio"); if (!string.IsNullOrWhiteSpace(notify.Audio)) { if (notify.Audio.ToUpperInvariant().Equals("NONE")) { audio.SetAttribute("silent", "true"); } else { audio.SetAttribute("src", notify.Audio); } toastNode.AppendChild(audio); } ((XmlElement)toastNode).SetAttribute( "launch", "{\"Type\":\"toast\",\"Id\":\"" + notify.Id + "\",\"Tag\":\"" + (notify.Tag ?? string.Empty) + "\"}"); var toast = new ToastNotification(toastXml); await this.dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { ToastNotificationManager.CreateToastNotifier().Show(toast); }); }