Toast Notifications in Win Forms .NET 4.5

9

I've searched through a number of different posts to do with creating toast notifications from a Win Form however when these through I get an error when generating the toast notification.

System.Exception: Element not found. (Exception from HRESULT:0x80070490).

I have edited the csproj file and added the following:

  <PropertyGroup>
       <TargetPlatformVersion>10.0.10586</TargetPlatformVersion>
  </PropertyGroup>

and added the references to Windows.Data and Windows.UI and also a reference to System.Runtime.dll as per the suggestions in Windows.UI.Notifications is missing

using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using System.Windows.Forms;
using System;

namespace ToastNotify
{
    class Notify
    {
        public void GenerateToast(string header, string content)
        {
            ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText02;

            XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);

            XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
            toastTextElements[0].AppendChild(toastXml.CreateTextNode(header));
            toastTextElements[1].AppendChild(toastXml.CreateTextNode(content));

            XmlNodeList toastImageElements = toastXml.GetElementsByTagName("image");
            ((XmlElement)toastImageElements[0]).SetAttribute("src", "..\\..\\Resources\\icon.ico");

            IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
            ((XmlElement)toastNode).SetAttribute("duration", "long");

            ToastNotification toast = new ToastNotification(toastXml);

            try
            {
                ToastNotificationManager.CreateToastNotifier().Show(toast);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());

            }
        }
    }
}

Any suggestions as to where I am going wrong?

c#
winforms
toast
asked on Stack Overflow Jul 17, 2018 by Etterz • edited Feb 23, 2021 by Joel Coehoorn

2 Answers

7

You should explicitly provide applicationId for CreateToastNotifier.

Like this:

private const String APP_ID = "Microsoft.Samples.DesktopToastsSample";
...
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);

But I have bad news. Starting from Windows 10 1709 WinForms applications just does not show toast notifications. Before that Show(toast) was working, but now it neither throw an exception, nor show any toast notification.

I'm still figuring this out.

As noted by Prateek Shrivastava there are (new) limitations.

Have a look here https://docs.microsoft.com/en-us/uwp/api/windows.ui.notifications.toastnotificationmanager.createtoastnotifier

Update:
Here is the step by step guide, to create a setup with APP_ID so notifications will work on all Windows 10 versions: Send a local toast notification from desktop C# apps

Update:
It works again in Windows 10 1903 without setup.

answered on Stack Overflow Sep 4, 2018 by Stepan Hakobyan • edited Jun 21, 2019 by Stepan Hakobyan
0

Use This and make sure you are setting the Image (Icon) full path if you want to show an icon otherwise just pass null.

public static void GenerateToast(string appid, string imageFullPath, string h1, string h2, string p1)
{

    var template = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04);

    var textNodes = template.GetElementsByTagName("text");

    textNodes[0].AppendChild(template.CreateTextNode(h1));
    textNodes[1].AppendChild(template.CreateTextNode(h2));
    textNodes[2].AppendChild(template.CreateTextNode(p1));

    if (File.Exists(imageFullPath))
    {
        XmlNodeList toastImageElements = template.GetElementsByTagName("image");
        ((XmlElement)toastImageElements[0]).SetAttribute("src", imageFullPath);
    }
    IXmlNode toastNode = template.SelectSingleNode("/toast");
    ((XmlElement)toastNode).SetAttribute("duration", "long");

    var notifier = ToastNotificationManager.CreateToastNotifier(appid);
    var notification = new ToastNotification(template);

    notifier.Show(notification);
}
answered on Stack Overflow Nov 28, 2020 by Mohammad Fneish

User contributions licensed under CC BY-SA 3.0