-1

The line of code that throws the error is:

new ToastContentBuilder()
    .AddText("Title of notification")
    .AddText("Body of notification")
    .Show();

The program is a Windows service made in C#, installed using a Wix installer. ToastContentBuilder comes from the Microsoft.Toolkit.Uwp.Notifications namespace. In Product.wxs, I have set the ServiceInstall tag as such:

<SeviceInstall Id="ServiceInstaller"
               Type="ownProcess"
               Vital="yes"
               Name="MyProcessName"
               DisplayName="My Process Name"
               Description="My process description."
               Start="auto"
               Account="LocalSystem"
               ErrorControl="normal"
               Arguments=""
               Interactive="yes" />

In the Services viewer in Windows, I have checked the properties of the process, and it is logged on as a Local System account, and the "Allow service to interact with desktop" box is checked.

I am aware of a similar question, but that question is unanswered and is seemingly not a service installed using Wix, so it is of little use.

I would appreciate any leads. I am not very familiar with Wix so something in Product.wxs may be at fault.

Tumblewood
  • 73
  • 2
  • 7
  • I answered the parts I could. I don't know that system can send toast to the user. For one, which session? For two, permissions. I guess the easy way to test is create a simple console.exe that can send a toast and then try running it as SYSTEM using PSEXEC. – Christopher Painter Sep 07 '21 at 20:09

1 Answers1

0

I've never tried to send a toast message from a windows service. I have sent toast messages from tray apps and scheduled tasks running as the user.

To have click events in the toast you have to create an app with a shortcut in the start menu like this:

<Shortcut Id="sc1" Name="Some App" Directory="ProgramMenuFolder" Description="Some App">
  <!--AUMID-->
  <ShortcutProperty Key="System.AppUserModel.ID" Value="WindowsNotifications.SomeApp" />
  <!--COM CLSID, specifying which CLSID to activate when toast clicked-->
  <ShortcutProperty Key="System.AppUserModel.ToastActivatorCLSID" Value="guid-used-in-code" />
</Shortcut>

In your code you have to implement

[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(INotificationActivationCallback))]
[Guid("guid-used-in-code"), ComVisible(true)]

and

    protected override void OnStartup(StartupEventArgs e)
    {
        // Register AUMID, COM server, and activator
        DesktopNotificationManagerCompat.RegisterAumidAndComServer<MyNotificationActivator>("WindowsNotifications.SomeApp");
        DesktopNotificationManagerCompat.RegisterActivator<MyNotificationActivator>();
Christopher Painter
  • 54,556
  • 6
  • 63
  • 100