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 6
badgeElement.SetAttribute("value", "6");
// Create a badge notification from XML
BadgeNotification 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 in
XmlNodeList 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 XML
TileNotification tileNotification = new TileNotification(tileXml);
// Send the notification to the secondary tile by creating a secondary tile updater
TileUpdater 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的機制不太一樣呢?!
