ToastNotification ToastNotification ToastNotification ToastNotification ToastNotification Class

Definition

Defines the content, associated metadata and events, and expiration time of a toast notification.

public : sealed class ToastNotification : IToastNotification, IToastNotification2, IToastNotification3, IToastNotification4
struct winrt::Windows::UI::Notifications::ToastNotification : IToastNotification, IToastNotification2, IToastNotification3, IToastNotification4
public sealed class ToastNotification : IToastNotification, IToastNotification2, IToastNotification3, IToastNotification4
Public NotInheritable Class ToastNotification Implements IToastNotification, IToastNotification2, IToastNotification3, IToastNotification4
var toastNotification = new toastNotification(content);
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

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;
    }
}

Remarks

A desktop app must subscribe to at least the Activated event to handle activation.

Constructors

ToastNotification(XmlDocument) ToastNotification(XmlDocument) ToastNotification(XmlDocument) ToastNotification(XmlDocument) ToastNotification(XmlDocument)

Creates and initializes a new instance of the ToastNotification.

Properties

Content Content Content Content Content

Gets the XML that defines the current toast notification.

Data Data Data Data Data

Gets or sets additional information about the status of the toast notification.

ExpirationTime ExpirationTime ExpirationTime ExpirationTime ExpirationTime

Gets or sets the time after which a toast notification should not be displayed.

Group Group Group Group Group

Gets or sets the group identifier for the notification.

NotificationMirroring NotificationMirroring NotificationMirroring NotificationMirroring NotificationMirroring

Gets or sets a value that specifies whether notification mirroring is allowed.

Priority Priority Priority Priority Priority

Gets or sets the priority of the toast notification.

RemoteId RemoteId RemoteId RemoteId RemoteId

Gets or sets a remote id for the notification that enables the system to correlate this notification with another one generated on another device.

SuppressPopup SuppressPopup SuppressPopup SuppressPopup SuppressPopup

Gets or sets whether a toast's pop-up UI is displayed on the user's screen.

Do not set this property to true in a toast sent to a Windows 8.x device. Doing so will cause a compiler error or a dropped notification.

Tag Tag Tag Tag Tag

Gets or sets the unique identifier of this notification within the notification Group.

Events

Activated Activated Activated Activated Activated

Occurs when user activates a toast notification through a click or touch. Apps that are running subscribe to this event

.

Dismissed Dismissed Dismissed Dismissed 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 Failed Failed Failed Failed

Occurs when an error is caused when Windows attempts to raise a toast notification. Apps that are running subscribe to this event

.

See Also