Introduction
Badge is a type of Push Notification available in Windows 8. It is displayed over the top of a tile. Badges are small icons that are shown in the lower right corner of a tile. The tile can either be a default tile or a tile with a notification.
A badge can display either a number from 1-99 or a status glyph. Now it is to be noticed that 99 is the largest number to be displayed as a badge. Even if you provide a number larger than 99, it will show badge notification as 99 only.You can have a full overview about number badges and glyph badges from here.
Using the code
Displaying a badge is similar to displaying a notification. So at first the following namespaces should be included:
Using Windows.UI.Notifications;
Using Windows.Data.Xml.Dom;
The following code is used for displaying a badge in Windows 8 metro App :
XmlDocument xmldoc = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeNumber);
XmlElement textnode = (XmlElement)xmldoc.SelectSingleNode("/badge");
textnode.SetAttribute("value", "available");
BadgeNotification not = new BadgeNotification(xmldoc);
BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(not);
Now we will learn what the following statements will do.The first line gets the blank XML badge template for a numeric badge. For a glyph badge, the code will be:
XmlDocument xmldoc = BadgeUpdateManager.GetTemplateContent(BadgeTemplateType.BadgeGylph);
The schema for both types of badges are same i.e. <badge value=””/>
The next two lines set the value attribute in the schema.
For the number badge this will be one or two digits. For the glyph badge it will be one of the following values: none, activity, alert, available, away, busy,
newMessage, paused, playing, unavailable, error.
The last two lines packages the XML into a notification and creates a badge object and display it on the tile.
Points of Interest
The Badge Notification will look like this in the tile:
The first image shows numeric badge while the second one shows gylph badge
We can also remove the badge notification with the help of following code:
BadgeUpdateManager.CreateBadgeUpdaterForApplication().Clear();
Now you all must be thinking that what is the usefulness of this badge or where this badge needs to be used?????
A Badge can be used –
- To show no. of unread mails in a mail application
- To show no. of online members in a chat
- To display the chat status
- To display the current level of game being played