Windows 8学习笔记(十)----Notification

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的机制不太一样呢?!

你可能感兴趣的:(notification)