How to make a notification from a C# Desktop Application in Windows 10

As it seems Microsoft “growls” are called toasts and i like them. I really hope that a lot desktop applications will use this new API to have a single place for all the notifications. Microsoft provides a Quickstart page with a some information and also an example for desktop applications in C++ and C#.

Screenshot of the example Application

I took an look at the C# example and everything looked just as expected, except for a piece of code that ensures the existence of a shortcut for the application on the Startmenu, which i find is a weird prerequisite for making toasts:

In order to display toasts, a desktop application must have a shortcut on the Start menu.
Also, an AppUserModelID must be set on that shortcut.
The shortcut should be created as part of the installer.

Anyway i wanted to try it out but couldn’t because the code that creates this shortcut requires the Windows API Code Pack to be available.

What it did then was just to cut this shortcut code out and try if it worked and it does!

So here is the stripped version of the official Microsoft example that worked just fine for me:

using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
 
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;
 
namespace DesktopToastsSample
{
    public partial class MainWindow : Window
    {
        private const String APP_ID = "Microsoft.Samples.DesktopToastsSample";
        public MainWindow()
        {
            InitializeComponent();
            ShowToastButton.Click += ShowToastButton_Click;
        }
 
        // Create and show the toast.
        // See the "Toasts" sample for more detail on what can be done with toasts
        private void ShowToastButton_Click(object sender, RoutedEventArgs e)
        {
 
            // 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");
            imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;
 
            // Create the toast and attach event listeners
            ToastNotification toast = new ToastNotification(toastXml);
            toast.Activated += ToastActivated;
            toast.Dismissed += ToastDismissed;
            toast.Failed += ToastFailed;
 
            // Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
            ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
        }
 
        private void ToastActivated(ToastNotification sender, object e)
        {
            Dispatcher.Invoke(() =>
            {
                Activate();
                Output.Text = "The user activated the toast.";
            });
        }
 
        private void ToastDismissed(ToastNotification sender, ToastDismissedEventArgs e)
        {
            String outputText = "";
            switch (e.Reason)
            {
                case ToastDismissalReason.ApplicationHidden:
                    outputText = "The app hid the toast using ToastNotifier.Hide";
                    break;
                case ToastDismissalReason.UserCanceled:
                    outputText = "The user dismissed the toast";
                    break;
                case ToastDismissalReason.TimedOut:
                    outputText = "The toast has timed out";
                    break;
            }
 
            Dispatcher.Invoke(() =>
            {
                Output.Text = outputText;
            });
        }
 
        private void ToastFailed(ToastNotification sender, ToastFailedEventArgs e)
        {
            Dispatcher.Invoke(() =>
            {
                Output.Text = "The toast encountered an error.";
            });
        }
    }
}

You can download the whole project folder, but bear in mind that this is not how Microsoft intended to use this (for whatever reason).

4 thoughts on “How to make a notification from a C# Desktop Application in Windows 10

  1. Thanks, I can use these in my fake anti-virus to warn the user they have 3500 or something like that viruses!

  2. You are using the old templates for notifications, the code sample you used was for Windows 8(.1), hence the requirement for an AppUserModelID (which is mandatory in W8(.1)). Now, this is easier to do (see the posts on http://blogs.msdn.com/b/tiles_and_toasts/ to have a good overview of all the possibilities) but unfortunately, no code sample for desktop app has been released for now.

Leave a Reply

Your email address will not be published. Required fields are marked *