Partager via


How to schedule a toast notification (XAML)

Note  Not using C#/VB/C++? See How to schedule a toast notification (HTML).

 

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

What you need to know

Technologies

  • Windows Runtime

Prerequisites

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. The code to fill in the template is omitted for clarity.

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

// TO DO: Fill in the template with your notification content here. 
 
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);