How to schedule a toast notification (Windows Store apps using JavaScript and HTML)

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

This topic shows how to 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: Specify a template

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



var template = Windows.UI.Notifications.ToastTemplateType.toastText02;                        
var toastXml = Windows.UI.Notifications.ToastNotificationManager.getTemplateContent(template);

Step 2: 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 3: Specify the time that the toast notification should be delivered

This example specifies that the notification should appear in 3 seconds. This example uses the JavaScript Date object to retrieve the current time.



var currentTime = new Date();
var startTime = new Date(currentTime.getTime() + 3 * 1000);

Step 4: Create the scheduled toast notification object

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


var scheduledToast = new Windows.UI.Notifications.ScheduledToastNotification(toastXml, startTime);

Step 5: 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 6: Add your toast notification to the schedule.

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



var toastNotifier = Windows.UI.Notifications.ToastNotificationManager.createToastNotifier();
toastNotifier.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.



var template = Windows.UI.Notifications.ToastTemplateType.toastText02;
var toastXml = Windows.UI.Notifications.ToastNotificationManager.getTemplateContent(template);

// TO DO: Fill in the template with your notification content. 
 
var currentTime = new Date();
var startTime = new Date(currentTime.getTime() + 1000);
 
var recurringToast = new Windows.UI.Notifications.ScheduledToastNotification(toastXml, startTime, 60 * 1000, 5);
recurringToast.id = "Recurring_Toast";

var toastNotifier = Windows.UI.Notifications.ToastNotificationManager.createToastNotifier();
toastNotifier.addToSchedule(recurringToast);

 

 

Build date: 11/29/2012

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.