23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A scheduled toast is created using Windows.UI.Notifications.ScheduledToast-Notification<br />

instead of the usual ToastNotification as we’ve been using. There are two forms of scheduled<br />

notification, as indicated by its pair of constructors:<br />

• ScheduledToastNotification(content, deliveryTime) Creates a one-time scheduled toast<br />

with the toast’s XmlDocument in content and the UTC DateTime when it should appear in<br />

deliveryTime.<br />

• ScheduledToastNotification(content, deliveryTime, snoozeInterval, maximumSnoozeCount)<br />

Creates a recurring scheduled toast whose content will appear at deliveryTime. If the toast is<br />

dismissed either explicitly or by letting it disappear on its own, it will continue to appear a total<br />

of maximumSnoozeCount times at intervals defined by the number of milliseconds in<br />

snoozeInterval. The snoozeInterval must be set between 60 seconds and 60 minutes; for<br />

longer intervals it’s best to just schedule separate toasts altogether.<br />

A ScheduledToastNotification also has an id property, a maximum 16-character string that’s used<br />

to identify that toast. If you schedule a toast with the same id as an existing one, the new one will<br />

replace the old.<br />

In all cases, the toast is scheduled by calling the ToastUpdater.addToSchedule method passing in the<br />

notification object. Here’s the process in code, as found Scenario 1 of the Scheduled notifications<br />

sample (js/scenario1.js), where toastDOM is the XmlDocument containing the content and dueTime is<br />

determined by a UI control in the sample. First, for a one-time notification:<br />

var Notifications = Windows.UI.Notifications;<br />

toast = new Notifications.ScheduledToastNotification(toastDOM, dueTime);<br />

Notifications.ToastNotificationManager.createToastNotifier().addToSchedule(toast);<br />

Second, for a notification that will repeat five times at 60-second intervals (the option that’s exercised<br />

if you check the Repeat checkbox in the sample’s UI):<br />

var Notifications = Windows.UI.Notifications;<br />

toast = new Notifications.ScheduledToastNotification(toastDOM, dueTime, 60 * 1000, 5);<br />

Notifications.ToastNotificationManager.createToastNotifier().addToSchedule(toast);<br />

To enumerate currently scheduled toasts, call ToastNotifier.getScheduledToast-Notifications.<br />

This returns a vector of ScheduledToastNotification objects, any of which can be canceled through<br />

ToastNotifier.removeFromSchedule. These methods are demonstrated in Scenario 2 of the Scheduled<br />

notifications sample that I will leave you to examine more closely. Also, there are some debugging tips<br />

on the Guidelines and checklist for scheduled notifications topic in the documentation, mostly to note<br />

that the system has a limit of 4096 total notifications and to make sure you’ve set Toast Capable in the<br />

manifest to Yes.<br />

605

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!