Defines the content, associated metadata and events, and expiration time of a toast notification.
Syntax
var toastNotification = new Windows.UI.Notifications.ToastNotification();
Attributes
- ActivatableAttribute(Windows.UI.Notifications.IToastNotificationFactory, NTDDI_WIN8)
- DualApiPartitionAttribute()
- ThreadingAttribute(MTA)
- VersionAttribute(NTDDI_WIN8)
Members
The ToastNotification class has these types of members:
Constructors
The ToastNotification class has these constructors.
| Constructor | Description |
|---|---|
| ToastNotification | Creates and initializes a new instance of the ToastNotification. |
Events
The ToastNotification class has these events.
| Event | Description |
|---|---|
| Activated | Occurs when user activates a toast notification through a click or touch. Apps that are running subscribe to this event. |
| Dismissed | Occurs when a toast notification leaves the screen, either by expiring or being explicitly dismissed by the user. Apps that are running subscribe to this event. |
| Failed | Occurs when an error is caused when Windows attempts to raise a toast notification. Apps that are running subscribe to this event. |
Methods
The ToastNotification class inherits methods from the Object class (C#/VB/C++).
Properties
The ToastNotification class has these properties.
| Property | Access type | Description |
|---|---|---|
| Read-only | Gets the XML that defines the current toast notification. | |
| Read/write | Gets or sets the time after which a toast notification should not be displayed. |
Remarks
A desktop app must subscribe to at least the Activated event to handle activation.
Examples
The following example shows how to create and send a toast notification that includes text and images, including use of the ToastNotification constructor.
var notifications = Windows.UI.Notifications; // Get the toast notification manager for the current app. var notificationManager = notifications.ToastNotificationManager; // The getTemplateContent method returns a Windows.Data.Xml.Dom.XmlDocument object // that contains the toast notification XML content. var template = notifications.toastTemplateType.toastImageAndText01; var toastXml = notificationManager.getTemplateContent(notifications.ToastTemplateType[template]); // You can use the methods from the XML document to specify the required elements for the toast. var images = toastXml.getElementsByTagName("image"); images[0].setAttribute("src", "images/toastImageAndText.png"); var textNodes = toastXml.getElementsByTagName("text"); textNodes.forEach(function (value, index) { var textNumber = index + 1; var text = ""; for (var j = 0; j < 10; j++) { text += "Text input " + /*@static_cast(String)*/textNumber + " "; } value.appendChild(toastXml.createTextNode(text)); }); // Create a toast notification from the XML, then create a ToastNotifier object // to send the toast. var toast = new notifications.ToastNotification(toastXml); notificationManager.createToastNotifier().show(toast);
The following example shows how to listen for and handle the Dismissed event.
var notifications = Windows.UI.Notifications; yourToastNotification.addEventListener("dismissed", function (e) { switch (e.reason) { case notifications.ToastDismissalReason.applicationHidden: // The application hid the toast using ToastNotifier.hide. break; case notifications.ToastDismissalReason.userCanceled: // The user dismissed the toast. break; case notifications.ToastDismissalReason.timedOut: // The toast has expired. break; } }
Requirements
|
Minimum supported client | Windows 8 [Windows Store apps, desktop apps] |
|---|---|
|
Minimum supported server | Windows Server 2012 [Windows Store apps, desktop apps] |
|
Namespace |
|
|
Metadata |
|
See also
- Toast notifications sample
- Sending toast notifications from desktop apps sample
- Toast XML schema
- Toast notification overview
- Quickstart: Sending a toast notification
- Quickstart: Sending a toast push notification
- Quickstart: Sending a toast notification from the desktop
- Guidelines and checklist for toast notifications
- How to handle activation from a toast notification
- How to opt in for toast notifications
- How to schedule a toast notification
- How to enable desktop toast notifications through an AppUserModelID
- The toast template catalog
- Toast audio options
Build date: 12/4/2012