Language: JavaScript and HTML | VB/C#/C++ and XAML
4 out of 5 rated this helpful - Rate this topic

How to schedule a toast notification

You can schedule a toast notification to appear at a specific time.

What you need to know

Technologies

  • Windows Runtime

Prerequisites

  • A working knowledge of toast notification terms and concepts. For more information, see Toast overview.
  • Ability to create a basic Windows Store app with JavaScript using Windows Runtime APIs.
  • The Toast Capable option must be set to "Yes" in your app's manifest to send or receive toast notifications. For more information, see How to opt in for toast notifications.

Instructions

Step 1: Add namespace declarations

Windows.UI.Notifications includes the toast APIs.



using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;

Step 2: Specify a template

Before you can specify the delivery time, you must create the notification.



ToastTemplateType toastTemplate = ToastTemplateType.ToastText02; 
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);

Step 3: Provide toast notification content

We won't cover this here because it's the same for a scheduled toast as for a non-scheduled toast. For more information, see Quickstart: Sending a toast notification.

Step 4: Specify the time that the toast notification should be delivered

This example specifies that the notification should appear in 3 hours. This example uses the DateTime object.



Int16 dueTimeInHours = 3;
DateTime dueTime = DateTime.Now.AddHours(dueTimeInHours);

Step 5: Create the scheduled toast notification object

Send the toast notification content and the scheduled delivery time to the constructor.



ScheduledToastNotification scheduledToast = new ScheduledToastNotification(toastXml, dueTime);

Step 6: Optional: Give the scheduled toast notification an ID

This ID must be 16 characters or less. It can be used later if you want to cancel the notification.


scheduledToast.Id = "Future_Toast";

Step 7: Add your toast notification to the schedule.

Create the ToastNotifier object, which in turn is used to add your notification to the schedule.



ToastNotificationManager.CreateToastNotifier().AddToSchedule(scheduledToast);

Adding a toast notification that repeats at a specific interval

The following code displays a single toast notification five times, each one minute apart, starting in three hours.



ToastTemplateType toastTemplate = ToastTemplateType.ToastText02; 
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);

// Fill in the template with your notification content. 
 
Int16 startTimeInHours = 3;
DateTime startTime = DateTime.Now.AddHours(startTimeInHours);
 
ScheduledToastNotification recurringToast = new ScheduledToastNotification(toastXml, startTime, 60 * 1000, 5);
recurringToast.Id = "Recurring_Toast";

ToastNotificationManager.CreateToastNotifier().AddToSchedule(recurringToast);

 

 

Build date: 11/29/2012

© 2013 Microsoft. All rights reserved.