Skip to main content
A newer version of this page is available. .

Toast Notification Manager

  • 6 minutes to read

The Toast Notification Manager component shows toast notifications - a Windows 10 version of traditional alert windows.

Toast Notifications - Overview

Note

Toast notifications can only be displayed in Windows 8.0 or higher. For older Windows versions, use alert windows instead.

An application can display multiple notifications simultaneously, or one notification multiple times at once. The ToastNotification object which represents a notification can be visualized using one of nine predefined templates. The notification can also play a sound when shown.

Create Notifications

Follow the steps below to create toast notifications.

  1. Locate the ToastNotificationManager component in your Visual Studio toolbox and drop it onto the form.

    ToastNotifications - Component

  2. Windows allows toast notifications only for those apps that are pinned to the Start menu, so make sure your application has a valid shortcut with an Application User Model ID. For debugging purposes, invoke the component’s smart tag and click the “Create Application Shortcut” link.

    Toast Notifications - Create shortcut

  3. Invoke the manager’s smart-tag and click the Edit Notifications… link.

    Toast Notifications - Collection editor

  4. In the Collection Editor dialog that pops up, click the Add button to add a notification. This creates new ToastNotification objects and add them to the manager’s ToastNotificationsManager.Notifications collection. You can customize the notification settings in the property grid.

  5. To display a specific notification, use the ToastNotificationsManager.ShowNotification method.

    
    toastNotificationsManager1.ShowNotification(toastNotificationsManager1.Notifications[3]);
    //or
    toastNotificationsManager1.ShowNotification("3b7fcd8b-a1e0-4ff5-83ce-023cdf6be24b");
    

Manage End-User Interactions

Depending on end-user actions, the following events rise.

  • ToastNotificationsManager.Activated - occurs if an end-user clicks this notification. Handle this event to check which notification was clicked, and perform specific actions depending on the result. The code below illustrates an example.

    
    private void toastNotificationsManager1_Activated(object sender, DevExpress.XtraBars.ToastNotifications.ToastNotificationEventArgs e) {
        switch (e.NotificationID.ToString()) {
            case "3b7fcd8b-a1e0-4ff5-83ce-023cdf6be24b":
                MessageBox.Show("Notification #1 Clicked");
                break;
            case "66501f90-ac6b-440d-bf73-483c5ab22143":
                MessageBox.Show("Notification #2 Clicked");
                break;
        }
    }
    
  • ToastNotificationsManager.UserCancelled - occurs if an end-user closes the notification.
  • ToastNotificationsManager.TimedOut - occurs when an end-user does not respond to the notification, and the notification is hidden after a certain period. The code below illustrates how to show the notification again.

    
    private void toastNotificationsManager1_TimedOut(object sender, DevExpress.XtraBars.ToastNotifications.ToastNotificationEventArgs e) {
        toastNotificationsManager1.ShowNotification(e.NotificationID);
    }
    
  • ToastNotificationsManager.Hidden - occurs when a toast notification is hidden via the ToastNotificationsManager.HideNotification or ToastNotificationsManager.HideNotifications method.
  • ToastNotificationsManager.Dropped - fires when a notification is canceled due to end-user’s system settings.

Customize Notifications with the “Generic” Template

The following XML markup describes the layout of a toast notification’s content with the “Generic” template applied:


<toast displayTimestamp="2018-01-05T13:35:00Z">
  <visual>
    <binding template="ToastGeneric">
      <text id="1">Header Text</text>
      <text id="2">Body Text</text>
      <text id="3">Body 2 Text</text>
      <text placement="attribution">Attribution Text</text>
      <image src="file:///C:/Users/John.Doe/AppData/Local/Temp/tmpBC2C.tmp4e9214ef-f478-4cea-972a-3fdd6c3acac0.png" placement="appLogoOverride" hint-crop="circle" />
      <image src="file:///C:/Users/John.Doe/AppData/Local/Temp/tmpBC2D.tmpeb4a5986-fd2a-4d7d-a69d-a78f0061d754.png" placement="hero" />
      <image src="file:///C:/Users/John.Doe/AppData/Local/Temp/tmpBC1B.tmp43598461-7e59-4600-a95c-88edbc57b2ec.png" />
    </binding>
  </visual>
</toast>

You can handle the ToastNotificationsManager.UpdateToastContent event to modify this template using the API the System.XML namespace provides. For instance, the following code adds a group with two subgroups to the notification layout. Each subgroup displays two additional text blocks, arranged vertically.


using System.Xml;

 public Form1() {
     InitializeComponent();
     //. . .
     toastNotificationsManager1.UpdateToastContent += ToastNotificationsManager1_UpdateToastContent;    
 }

 private void ToastNotificationsManager1_UpdateToastContent(object sender, DevExpress.XtraBars.ToastNotifications.UpdateToastContentEventArgs e) {
     XmlDocument content = e.ToastContent;
     XmlNode bindingNode = content.GetElementsByTagName("binding").FirstOrDefault();
     XmlElement group = content.CreateElement("group");
     bindingNode.AppendChild(group);

     XmlElement subGroup = content.CreateElement("subgroup");
     group.AppendChild(subGroup);

     XmlElement text = content.CreateElement("text");
     subGroup.AppendChild(text);
     text.SetAttribute("hint-style", "base");
     text.InnerText = "subgroup1";

     text = content.CreateElement("text");
     subGroup.AppendChild(text);
     text.SetAttribute("hint-style", "captionSubtle");
     text.InnerText = "captionSubtle";

     subGroup = content.CreateElement("subgroup");
     group.AppendChild(subGroup);

     text = content.CreateElement("text");
     subGroup.AppendChild(text);
     text.SetAttribute("hint-style", "captionSubtle");
     text.SetAttribute("hint-align", "right");
     text.InnerText = "subgroup2";

     text = content.CreateElement("text");
     subGroup.AppendChild(text);
     text.SetAttribute("hint-style", "captionSubtle");
     text.SetAttribute("hint-align", "right");
     text.InnerText = "captionSubtle";
     //save the toast markup as an XML file for debugging purposes
     content.Save(@"D:\Toast.xml");
 }

Toast Notifications - Custom Template

Notes

  • The notification background color is chosen automatically by the operating system, you cannot modify it manually.
  • Notifications are displayed by the operating system, so they can remain visible even after an end-user closes the application.
  • Windows 8-styled notifications automatically display a shortcut icon in the bottom right corner. This icon cannot be removed.
  • The number of notifications that a visible simultaneously depends on the end-user’s system settings.
  • Windows 10 Anniversary Update (Redstone 1, build 1607) and newer versions support the Generic notification template.