Toast notifications for Windows 8

Toast notifications are notifications sent from the app to the user. Nevertheless which app you are in, the notifications appear from top right corner, notifying you of an event or a reminder. Toasts can have images and texts but no secondary actions such as buttons. Clicking on a toasts will take the user to the respective app and is the only method by which one app can interrupt the other.

To make things easier, Microsoft provides us with 8 templates from which we can choose the one which fits our needs.

Now lets take a look at creating ToastImageAndText02

First we create a string ‘toastXmlString‘ which basically the string from which xml will be created. Here ‘alarmHeader’ will hold the heading of the toast. and ‘alarmDescription‘ will be the text following the heading.

alarmHeader = "My alarm";
alarmDescription = "The alarm will be displayed as a toast";

string toastXmlString =
“<toast duration=\”long\”>\n” +
“<visual>\n” +
“<binding template=\”ToastText02\”>\n” +
“<text id=\”1\”>” + alarmHeader+ “</text>\n” +
“<text id=\”2\”>” + alarmDescription + “</text>\n” +
“</binding>\n” +
“</visual>\n” +
“<commands scenario=\”alarm\”>\n” +
“<command id=\”snooze\”/>\n” +
“<command id=\”dismiss\”/>\n” +
“</commands>\n” +
“<audio src=\”ms-winsoundevent:Notification.Looping.Alarm2\” loop=\”true\” />\n” +

<text id> 1 and 2 are the text we provide, while <command id> is the action which can be performed when an toast pops up, and in this alarm scenario, it is snooze and dismiss. <audio src> is the source of the audio file which needs to be played when the alarm pops up.

Our string is ready and now we need to create an xml from it.

var toastDOM = new Windows.Data.Xml.Dom.XmlDocument();

We need an object that will raise the toast for the application.

var toastNotifier = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();

We are all set to schedule this toast to appear at a particular time. Here ‘date’ is a DateTime variable which has been set to particular date and time.

var customAlarmScheduledToast = new Windows.UI.Notifications.ScheduledToastNotification(toastDOM, date);

Its not finished yet. You need to go to the manifest file either from the solution explorer or click
Store ->Edit Manifest

In the application tab, under Notifications heading, Make the Toast capable option to ‘Yes‘. And you are good to go!

The toast is out of the toaster 😀

You can also create the toast through  standard DOM functions:. I have provided MSDN links describing the same

May the code be with you! 😀

chops-logo2 small