Toast Notifications are not displaying outside IDE debugger

1

I have build an app that is supposed to show Toast Notifications on Windows 10, every time a FileSystemWatcher detects a change. I am using the Microsoft.Toolkit.Uwp.Notifications and Windows.UI.Notifications namespaces. When running the code in VS2019 with the debugger enabled, it works flawlessly, but after installing the application using a self-written WiX installer, I get an error message after which the app closes.

The error message is pretty generic, and I am out of clues as to where to look:

Failed to show Notification for [FileSystemWatcher Name].
Element Not Found. (Exception from HRESULT: 0x80070490)
at Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier()
at ...
at .. rest of callstack is from the app, pointing towards the method with CreateToast

The class that is responsible for creating and showing the toasts

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

namespace MyApp.Notifs
{
    public class NotificationToaster
    {

        private string appID;

        //appID is read from the Properties
        public NotificationToaster(string appID)
        {
            this.appID = appID;
        }

        public void ShowToast(string title, string content)
        {

            string toastXmlString =
            $@"<toast>
                    <visual>
                        <binding template=""ToastGeneric"">
                            <text hint-maxLines=""1""> {title} </text>
                            <text> {content} </text>
                            <group>
                                <subgroup>
                                    <text> {DateTime.Now} </text>
                                </subgroup>
                            </group>
                        </binding>
                    </visual>
                    <audio src=""ms-winsoundevent:Notification.Default""/>
                </toast>";

            CreateToast(toastXmlString);

        }

        private void CreateToast(string toast)
        {
            //Load Toast data from string to XML
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(toast);

            // Create the toast notification
            ToastNotification toastNotif = new ToastNotification(xmlDoc);

            //Attach Manager to raise notifications
            var toastNotifier = ToastNotificationManager.CreateToastNotifier(appID);

            // And send the notification
            toastNotifier.Show(toastNotif);
        }

    }
}

After a bit of searching, I've tried several solutions.

One of the first things i tried, was packaging the app in a Windows Application Packaging Project, as described here. (I haven't ported the WPF project from Framework to Core yet though.)

Older posts here on SO suggest to use an Application ID as param when creating the Notifier. Doing this with a static GUID, it stops sending notifications, while all breakpoitns are hit. Outside of debugging the program doesn't crash anymore, but notifications aren't being send either.

Any pointer as to what might be wrong here would be of huge help.

c#
wpf
toast
asked on Stack Overflow Jan 22, 2020 by DemonWyvern

1 Answer

1

If you don't package your app, you have to declare your Application User Model ID (AUMID) and toast activator CLSID on your app's shortcut in Start as described in the docs:

<Shortcut Id="ApplicationStartMenuShortcut" Name="Wix Sample" Description="Wix Sample" Target="[INSTALLFOLDER]WixSample.exe" WorkingDirectory="INSTALLFOLDER">

<!--AUMID-->
<ShortcutProperty Key="System.AppUserModel.ID" Value="YourCompany.YourApp"/>

<!--COM CLSID-->
<ShortcutProperty Key="System.AppUserModel.ToastActivatorCLSID" Value="{replaced-with-your-guid-C173E6ADF0C3}"/>

answered on Stack Overflow Jan 24, 2020 by mm8

User contributions licensed under CC BY-SA 3.0