Derik Whittaker

Syndication

News


Sending In-Application Toast Notifications in WinRT for C#

When building a Windows RT application (based on C#) it is pretty common that you will want to notify your user of an event.  It is more common that you are going to want to do this in a way which is not intrusive, aka does not get in the way of the user.  Luckily for you the WinRT team has built a great toast notification engine into the framework.

In fact this notification engine allows for toasts to be both in-application as well as remote notifications.  It also allows for notifications to be real time (sent when at the time you create them) as well as scheduling them to be sent at some predetermined date/time in the future.

In this post I am only going to show how to do in-application toasts, I will focus on cloud base toasts in a future posts.  Before we dive into the code, take a second to look at the Toast Template Catalog for all the various ways we can layout/format the toast.

Getting started.

What does a toast look like?

 

 

How do create toasts?

  1. You must enable toasts via the Package.appxmanifest

    image 

    If you do not enable toasts you will NOT get a toasts, nor will you get an error, it will simply NOT WORK.
  2. Check to see if toasts have been enabled
                
    var notifier = ToastNotificationManager.CreateToastNotifier();
    if (notifier.Setting != NotificationSetting.Enabled)
    {
    	// do something here
    }            
    
    	

    In the above we are first creating the notifier, then we simply check the Setting property. If it is not enabled you may want to punt or alert the user.

  3. Create the code to create the toasts – Real time toasts
                
    var notifier = ToastNotificationManager.CreateToastNotifier();
    var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
                
    var element = template.GetElementsByTagName("text")[0];
    element.AppendChild(template.CreateTextNode("Line 1 goes here"));
    
    var toast = new ToastNotification(template);
    notifier.Show(toast);
    	

    If you look at the code above one thing should jump out at you right way, the fact you have to drop down to raw XML.  Yea, it sucks.  The toast team decided to NOT wrap this in the same way as the Tile team, but oh well.

    In the above we are first creating the notifier, then we will chose the template we want.  Once we do that we just start updating the values as expected.

  4. Create to code to create scheduled toasts
                
    var notifier = ToastNotificationManager.CreateToastNotifier();
    var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
                
    var element = template.GetElementsByTagName("text")[0];
    element.AppendChild(template.CreateTextNode("Line 1 goes here"));
    
    // Schedule the toast to appear 10 seconds from now
    var date = DateTimeOffset.Now.AddSeconds(10);
    var scheduledToast = new ScheduledToastNotification(template, date);
    notifier.AddToSchedule(scheduledToast);
    	

    If you look at the code above about the only thing that has changed is that we are now creating a ‘ScheduledToastNotification’ instance and adding this to the ToastNotifiier.

As you can see doing in-application toast notifications is very easy and only requires a few lines of code.

Till next time,


Posted 11-14-2012 5:52 PM by Derik Whittaker
Filed under: , , ,

[Advertisement]

Comments

Soroush Mirzaei wrote re: Sending In-Application Toast Notifications in WinRT for C#
on 01-20-2013 8:54 AM

Nice article, Thanks!

buy discount viagra wrote re: Sending In-Application Toast Notifications in WinRT for C#
on 02-02-2013 10:16 AM

4xm55V Thanks for sharing, this is a fantastic blog article.Really thank you! Much obliged.

buy discount viagra wrote re: Sending In-Application Toast Notifications in WinRT for C#
on 02-03-2013 12:00 PM

fSVTT0 This is one awesome blog.Really thank you!

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)