Share via


Quickstart: Sending a toast notification from the desktop

This quickstart shows how to raise a toast notification from a desktop app.

Prerequisites

Instructions

1. Create your toast content

Note  When you specify a toast template that includes an image, be aware that desktop apps can use only local images; web images are not supported. Also, the path to the local image file must be provided as an absolute (not relative) path.

 


// Get a toast XML template
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);

// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
for (int i = 0; i < stringElements.Length; i++)
{
    stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
}

// Specify the absolute path to an image
String imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");

ToastNotification toast = new ToastNotification(toastXml);

2. Create and attach the event handlers

Register handlers for the toast events: Activated, Dismissed, and Failed. A desktop app must at least subscribe to the Activated event so that it can handle the expected activation of the app from the toast when the user selects it.


toast.Activated += ToastActivated;
toast.Dismissed += ToastDismissed;
toast.Failed += ToastFailed;

3. Send the toast

Important  You must include the AppUserModelID of your app's shortcut on the Start screen each time that you call CreateToastNotifier. If you fail to do this, your toast will not be displayed.

 

ToastNotificationManager.CreateToastNotifier(appID).Show(toast);

4. Handle the callbacks

Bring your app's window to the foreground if it receives an "activated" callback from the toast notification. When a user selects a toast, the expectation is that the app will be launched to a view related to the content of that toast..

Summary and next steps

Sending toast notifications from desktop apps sample

How to enable desktop toast notifications through an AppUserModelID

Toast notifications sample

Toast XML schema

Toast notification overview

Quickstart: Sending a toast notification

Quickstart: Sending a toast push notification

Guidelines and checklist for toast notifications

How to add images to a toast template

How to check toast notification settings

How to choose and use a toast template

How to handle activation from a toast notification

How to opt in for toast notifications

Choosing a toast template

Toast audio options