Click here to Skip to main content
15,886,258 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Badge Notifications in Windows 8

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
28 Jun 2012CPOL2 min read 17.5K   5   1
It shows how to display a badge notification in Windows 8 metro app

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:

C#
Using Windows.UI.Notifications;
Using Windows.Data.Xml.Dom;

The following code is used for displaying a badge in Windows 8 metro App :

C#
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:

C#
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:

C#
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
VistaaR7-Aug-12 21:07
VistaaR7-Aug-12 21:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.