Search code examples
c#windowswinapiconsole-applicationtoast

C# Console Toast Stops Displaying Notification


About six weeks ago I developed a C# console app as a proof of concept to generate a Windows 10 toast notification. Worked fine. I revisit the solution only to find it builds and runs without errors, but the toast notification no longer displays. Have researched thoroughly on SO and so far no love. Hoping someone in the community can point me in the right direction, so here goes:

Environment

  • Windows 10, version 1709
  • .NET framework 4.6.1
  • Visual Studio 2017, version 15.7.4

.csproj file addition

<PropertyGroup> <TargetPlatformVersion>10.0.1709</TargetPlatformVersion> </PropertyGroup>

Added References

  • Windows.Data
  • Windows.UI
  • System.Runtime (from .NETFramework\v4.6.1\Facades)

Program.cs

using System;
using System.IO;
using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;

class Program
{
    private const String APP_ID = "CompanyName.Notifier.ToastName";

    static void Main(string[] args)
    {
        ToastContent content = new ToastContent()
        {
            Visual = new ToastVisual()
            {
                BindingGeneric = new ToastBindingGeneric()
                {
                    Children =
                    {
                        new AdaptiveText()
                        {
                            Text = "Desired text",
                            HintMaxLines = 1
                        },

                        new AdaptiveText()
                        {
                            Text = "More desired text"
                        },
                    },

                    HeroImage = new ToastGenericHeroImage()
                    {
                        Source = @"C:\Logo.png" // hard-coded path for testing
                    }
                }
            },

            Duration = ToastDuration.Long

        };

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(content.GetContent());

        var tstVisual = new ToastNotification(xmlDoc);

        ToastNotificationManager.CreateToastNotifier(APP_ID).Show(tstVisual);

    }
}

Additional Thoughts

I checked the XML generation (did a File.WriteAllText with the XML obtained with content.GetContent()) and the toast XML generates fine. It seems like something is preventing the ToastNotificationManager.CreateToastNotifierfrom displaying the toast notification. Thanks for looking at this.


Solution

  • The issue was caused by the Fall Creators Update for Windows 10 (version 1709). In prior versions of Windows 10 the Application User Model ID (AppID) had few restrictions. However, with version 1709, Microsoft now requires an existing AppID recognized by Windows 10 or a custom AppID created either via an AppxManifest or via creating a Windows Start Menu shortcut with he AppId embedded in it via XML. I chose the existing Powershell AppId and changed my code to

    private const string APP_ID = @"{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe";

    I stumbled upon the answer when I started researching how I might do a toast using Powershell. These links go into the issue and its resolution in greater detail:

    Toast Notification Not Working on Windows Fall Creators Update

    GitHub Gist on Using Powershell AppId for Windows Toast

    How to Create a Package Manifest Manually

    Update

    I ended up using WiX to create a custom Windows Start Menu shortcut with the AppID embedded in it via XML. (Using another application's AppID placed incorrect labeling in the Windows Action Center, among other issues). I found a very good blog post that detailed the key XML code and the steps needed to create an installer for the shortcut

    Josh King: DIY AppId and WiX, for Tasty Toast

    as well as the original WiX documentation on the subject

    WiX How To: Create a Shortcut on the Start Menu