Scheduling Toast Notifications

Excerpt by Ken Getz

You can schedule a toast to appear any time in the future-you supply a time at which you want the toast notification to appear, and it shows up on demand. Your application doesn't even have to be running! In order to better manage your toasts, however, you should set the Id property of the toast to a string of up to 16 characters. Having an ID allows you to refer to the toast (and possibly cancel it). Note that all the previous examples have used a ToastNotifier instance, but haven't stored a reference to it for later use. The ToastNotifier class provides several useful methods, and if you plan to work with toast notifications after they're created, you will need to create a specific reference to a ToastNotifier instance to work with. Specifically, the ToastNotifier class provides these members:

  • AddToSchedule, RemoveFromSchedule
  • Show (which you previously called), Hide
  • GetScheduledToastNotifications

To create a scheduled notification, start by creating an instance of the ScheduledToastNotification class. To its constructor, pass the XML content of a ToastContent instance-you can use the GetXml method to retrieve the XML content. Next, create the ToastNotifier instance, and call its AddToSchedule method, supplying the ScheduledToastNotification instance as a parameter. In addition, supply the time at which you want to display the notification. NOTEYou cannot schedule more than 4096 notifications. Also note thatyou cannot schedule a notification in the past (in other words, youcan't break the inviolable laws of the space-time continuum). Creating a Recurring Notification To create a recurring notification, follow the exact same steps as for a scheduled notification, except that you must also specify the snooze interval (that is, how long to wait before reattempting the notification), which can be a value between 60 seconds and 60 minutes. Also, specify the maximum occurrences of the notification (a value between 1 and 5). TIP:Specify an Id property when creating scheduled and/or recurring toastnotifications. Doing so will make it easier to identify notifications whenreviewing or cancelling them.


ldn-expertkgetzThis post is an excerpt from the online courseware for ourWindows 8 Using XAML: Views, Resources, and Toastscourse written by expert Ken Getz.


Ken Getz
Ken Getz is a featured instructor for several of our Visual Studio courses. He is a Visual Basic and Visual C# expert and has been recognized multiple times as a Microsoft MVP. Ken is a seasoned instructor, successful consultant, and the author or co-author of several best-selling books. He is a frequent speaker at technical conferences like Tech-Ed, VSLive, and DevConnections and he has written for several of the industry's most-respected publications including Visual Studio Magazine, CoDe Magazine, and MSDN Magazine.

This course excerpt was originally posted July 18, 2013 from the online courseware Windows 8 Using XAML, Part 09: Views, Binding, and Templates by Ken Getz

Windows 8 XAML