WinRT中Notification有三種類型:
Badge:徽章、標記的推送更新,主要對於磁磚進行通知
Toast:土司推送,一般是程式運行時通知
Tile:磁磚推送,自然就是程式處理休眠狀態時的通知
注意:這裡有訊息內容都是以XML內容範本發送的
先研究下磁磚的推送Badge和Tile
Badge
瞭解一下有哪些方法
BadgeNotification(XmlDocument content)
BadgeUpdateManager
CreateBadgeUpdaterForApplication()---為當前應用程式更新磁磚
CreateBadgeUpdaterForApplication(string applicationID)---為指定的應用程式更新磁磚
有個疑問,這裡的applicationID一般在哪兒得到??還望知道的告知一下哦~
CreateBadgeUpdaterForSecondaryTile(string tileID)—為指定的磁磚更新
GetTemplateContent(BadgeTemplateType type)—擷取預定義好的XML徽章模板
BadgeUpdater
Update(BadgeNotification notification)
範例程式碼:
XmlDocument badgeXml = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber);XmlElement badgeElement = (XmlElement)badgeXml.SelectSingleNode("/badge"); // We are setting attribute value 6badgeElement.SetAttribute("value", "6");// Create a badge notification from XMLBadgeNotification badgeNotification = new BadgeNotification(badgeXml);// Send the notification to the secondary tile BadgeUpdater badgeUpdater = BadgeUpdateManager.CreateBadgeUpdaterForSecondaryTile(MainPage.dynamicTileId); badgeUpdater.Update(badgeNotification);
Tile
它包含的方法跟Badge類似
TileNotification(XmlDocument document)
TileUpdateManager
包手中四個方法,跟BadgeUpdateManager類似
CreateTileUpdaterForApplication()
CreateTileUpdaterForApplication(string applicationID)
CreateTileUpdaterForSecondaryTile(string tileID)
GetTemplateContent
TileUpdater--Update(TileNotificaton notification)
範例程式碼:
XmlDocument tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideText04);// Refer to template documentation to determine how many text fields a particular template has // get the text attributes for this template and fill them inXmlNodeList tileTextElements = tileXml.GetElementsByTagName("text");tileTextElements.Item(0).AppendChild(tileXml.CreateTextNode("Sent to a secondary tile!"));XmlDocument squareTileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquareText04);XmlNodeList squareTileTextElements = squareTileXml.GetElementsByTagName("text");squareTileTextElements.Item(0).AppendChild(squareTileXml.CreateTextNode("Sent to a secondary tile!")); // Include the square template in the notification IXmlNode subNode = tileXml.ImportNode(squareTileXml.GetElementsByTagName("binding").Item(0), true); tileXml.GetElementsByTagName("visual").Item(0).AppendChild(subNode);// Create the notification from the XMLTileNotification tileNotification = new TileNotification(tileXml); // Send the notification to the secondary tile by creating a secondary tile updaterTileUpdater tileUpdater = TileUpdateManager.CreateTileUpdaterForSecondaryTile(MainPage.dynamicTileId);tileUpdater.Update(tileNotification);
Toast
方法和屬性也基本類似
但我下面的代碼有問題,沒有顯示Toast通知,註冊Failed事件,跟蹤說是應用程式功能會阻止通知傳遞。 (異常來自 HRESULT:0x803E0112)
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);XmlNodeList textNodes = toastXml.GetElementsByTagName("text"); textNodes.Item(0).AppendChild(toastXml.CreateTextNode("Sent to Toast Notification!"));ToastNotification toastNotification = new ToastNotification(toastXml);toastNotification.Failed += toastNotification_Failed;toastNotification.Dismissed += toastNotification_Dismissed;ToastNotifier notifier = ToastNotificationManager.CreateToastNotifier();
在MSDN Sample裡有Toast Sample樣本,裡面封裝了NotificationExtensions擴充,很容易就可以實現通知更新,不需要編輯複雜的XML模板,但很糾結,我上面的代碼有啥問題??
NotificationsExtensions微軟樣本給我們封裝的類庫,非常方便,得學習一下大師的封裝邏輯。。。
磁磚的更新還算簡單,PushNotification有點摸不著頭腦,首先PushChannel都是要手動輸入,這個不是產生的麼,很糾結,還希望有研究的同胞能提供點學習資料,跟Phone7的機制不太一樣呢?!