2

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.

joeschwa
  • 3,083
  • 1
  • 21
  • 41
  • Can you check your source control system and show us any changes made to the code recently? What is the value of `content.GetContent()`? – mjwills Jun 20 '18 at 21:42
  • Does it help if you add `Console.ReadLine();` to the end of the `Main` method? – mjwills Jun 20 '18 at 21:43
  • @mjwills No changes were made to the code. The solution was not even checked into TFS (as it was a POC). `Console.ReadLine();` has no affect. FYI, the Application Properties Output Type is `Windows Application` to prevent the console window from displaying. – joeschwa Jun 20 '18 at 21:51
  • 1
    Have you tried running this on another machine? Perhaps Windows 10 has installed some sort of update while you weren't looking that has torpedoed your code. – Paul Sanders Jun 21 '18 at 05:08
  • @PaulSanders I did indeed try my code on another Windows PC. No sale. Now it is still possible that a Windows update did hose my code, but the PCs I have access to all have the same updates and I do not have the authority to roll them back on my corporate desktop. – joeschwa Jun 21 '18 at 05:19

1 Answers1

3

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

joeschwa
  • 3,083
  • 1
  • 21
  • 41