ToastNotification class

Applies to Windows and Windows Phone

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

Syntax


var toastNotification = new Windows.UI.Notifications.ToastNotification();

Attributes

[DualApiPartition()]
[Threading(MTA)]
[Version(0x06020000)]

Members

The ToastNotification class has these types of members:

Constructors

The ToastNotification class has these constructors.

ConstructorDescription
ToastNotification Creates and initializes a new instance of the ToastNotification.

 

Events

The ToastNotification class has these events.

EventDescription
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.

 

Properties

The ToastNotification class has these properties.

PropertyAccess typeDescription

Content

Read-onlyGets the XML that defines the current toast notification.

ExpirationTime

Read/writeGets or sets the time after which a toast notification should not be displayed.

Group

Read/writeGets or sets the group identifier for the notification.

SuppressPopup

Read/writeGets or sets whether a toast's pop-up UI is displayed on the Windows Phone 8.1 screen.

Tag

Read/writeGets or sets the unique identifier of this notification within the notification Group.

 

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]

Minimum supported phone

Windows Phone 8.1 [Windows Phone Silverlight 8.1 and Windows Runtime apps]

Namespace

Windows.UI.Notifications
Windows::UI::Notifications [C++]

Metadata

Windows.winmd

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

 

 

Show:
© 2014 Microsoft