WindowsAppSDK
WindowsAppSDK copied to clipboard
Can't Send App notifications from (Admin) Console app using Windows apps sdk
Describe the bug
Can't Send App notifications from Console app using Windows apps sdk
Exception is thrown:
System.TypeInitializationException: The type initializer for 'WinRT.ActivationFactory1' threw an exception. ---> System.Runtime.InteropServices.COMException (0x80040154): Class not registered (0x80040154 (REGDB_E_CLASSNOTREG)) at WinRT.BaseActivationFactory..ctor(String typeNamespace, String typeFullName) at WinRT.ActivationFactory1..cctor()
--- End of inner exception stack trace ---
at Microsoft.Windows.AppNotifications.Builder.AppNotificationBuilder..ctor()
at Program.<Main>$(String[] args) in C:\Users\kwodarc\MyRepos\BugSandBox\ConsoleApp1\Program.cs:line 14
Steps to reproduce the bug
Create Console app and use latest windows apps sdk pacage and copy paste the following code and run:
// See https://aka.ms/new-console-template for more information
using Microsoft.Windows.AppNotifications.Builder;
using Microsoft.Windows.AppNotifications;
Console.WriteLine("Send Toast ?");
Console.ReadLine();
try
{
var appNotification = new AppNotificationBuilder()
.AddArgument("action", "ToastClick")
.AddText("Test Toast")
.AddText("This is an example message")
.BuildNotification();
AppNotificationManager.Default.Show(appNotification);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.230811000-preview2" />
</ItemGroup>
</Project>
Expected behavior
toast sent
Screenshots
NuGet package version
WinUI 3 - Windows App SDK 1.4 Preview 1: 1.4.230628000-preview1
Windows version
Windows 11 (22H2): Build 22621.2134
Additional context
No response
Wrong repo: https://github.com/microsoft/WindowsAppSDK
Do you initialize the Windows App SDK runtime before calling the functions? https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/use-windows-app-sdk-run-time
I just tried to use Windows App SDK on my existed C++ console project and I got the same error if I didn't call MddBootstrapInitialize firstly.
But my app is packaged not "packaged with external location"
I can understand the confusion since there was never any mention of the package. But the same kind of question applies for packaged applications. In the instructions above, there is no mention of adding the Windows App SDK as a reference to the packaging project.
It needs to be added to this too so the project system can properly add it as a reference to the MSIX package. You can determine if this is being added properly if you find entries similar to:
<Dependencies>
<TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.17763.0" MaxVersionTested="10.0.22621.0" />
<TargetDeviceFamily Name="Windows.Desktop" MinVersion="10.0.17763.0" MaxVersionTested="10.0.22621.0" />
<PackageDependency Name="Microsoft.WindowsAppRuntime.1.4" MinVersion="4000.964.11.0" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
<PackageDependency Name="Microsoft.VCLibs.140.00.Debug" MinVersion="14.0.30704.0" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
<PackageDependency Name="Microsoft.VCLibs.140.00.Debug.UWPDesktop" MinVersion="14.0.30704.0" Publisher="CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington, C=US" />
</Dependencies>
in your AppxManifest.xml file. So if you are using the .wapproj project, then be sure that you add the Windows App SDK as a reference to that. If you are manually making the MSIX package, then be sure that you also add the Windows App SDK reference to your AppxManifest.xml file.
@petercoin thank you for pointing me in the right direction, i had to add Bootstrap.Initialize(0x00010004); now i don't get any error but the toast is simple not sent are you able to sent toasts from command line application using wappsdk?
@KWodarczyk Yes, I can send the toast from my unpackaged console application written in C++ with winappsdk. Could you share about how did you create a packaged console app and then I can check it for you?
@petercoin sorry it's just standard console app in c# not 'packaged' or 'unpackaged' with added wappssdk
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows10.0.22000.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.230822000" />
</ItemGroup>
</Project>
static async Task Main(string[] args)
{
Bootstrap.Initialize(0x00010004);
var appNotification = new AppNotificationBuilder()
.AddArgument("action", "ToastClick")
.AddText("Test Toast")
.AddText("This is an example message using XML")
.BuildNotification();
AppNotificationManager.Default.Show(appNotification);
}
@KWodarczyk Because you use WinAppSDK v1.4, I would recommend to set
<WindowsPackageType>None</WindowsPackageType>
in your project file like
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows10.0.22000.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<WindowsPackageType>None</WindowsPackageType>
</PropertyGroup>
and then you will no need to call Bootstrap initialize by yourself. Besides, according to the link I mentioned before, you also need to call
AppNotificationManager::Default().Register();
for the unpackaged application before sending your notification. I just tried it on a newly created project, and it worked well.
@petercoin sorry it's just standard console app in c# not 'packaged' or 'unpackaged' with added wappssdk
If it isn't packaged, it is unpackaged.
this is may code and toast is not showing up
static async Task Main(string[] args)
{
AppNotificationManager.Default.Register();
var appNotification = new AppNotificationBuilder()
.AddArgument("action", "ToastClick")
.AddText("Test Toast")
.AddText("This is an example message using XML")
.BuildNotification();
AppNotificationManager.Default.Show(appNotification);
Console.ReadKey();
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows10.0.22000.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<WindowsPackageType>None</WindowsPackageType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.230822000" />
</ItemGroup>
</Project>
@KWodarczyk I cannot reproduce the issue with your code. You may try to update Windows APP SDK to the latest stable version which was just published to see if the issue still exists. One more thing, have you checked if the Focus Assist (Do not disturb) is enabled or not? When enabled, the toast notification will not be displayed too.
hold on, i'm running my app on admin account that is probably why, are you running yours on Admin or standard account ?
yeah when i used WCT notifications is it works fine, so looks line wappsdk notifications are not allowed admin account even if app is self is not elevated.
@KWodarczyk I just tried to use "Run as administrator" to execute my app and the notification won't work.
WinAppSDK does not allow elevated apps to show AppNotifications. https://github.com/microsoft/WindowsAppSDK/blob/18acdc725de089fe5d984522f614204dc03741b3/dev/AppNotifications/AppNotificationManager.cpp#L105
I'd imagine if you are running the app as an admin, the app itself will run with elevated privileges by default? You can check if AppNotifications are available at runtime using AppNotificationManager::IsSupported. https://learn.microsoft.com/en-us/windows/windows-app-sdk/api/winrt/microsoft.windows.appnotifications.appnotificationmanager.issupported?view=windows-app-sdk-1.4#microsoft-windows-appnotifications-appnotificationmanager-issupported
Ok I can also confirm that it works fine in Standard account, It's a pity that it will not work on Admin as this is probably majority of accounts that people run their PCs, if WCT supports it it's must not be that hard to enable it on wappsdk right ?
I just wrote a C# desktop packaged app that uses the WCT to build/receive activation from notification. Here's what I found:
- Packaged/Not elevated = Foreground process (COM server) receives the payload✅
- Packaged/Elevated = Foreground process (COM server) does not receive the payload, but instead a 2nd process opens to receive the payload as nonelevated. Now there are 2 instances of the process (elevated + non-elevated), and all notifications now routed to the non-elevated instance ❌
- Unpackaged/Not elevated = Foreground process (COM server) receives the payload✅
- Unpackaged/Elevated = Foreground process (COM server) receives the payload✅
WCT does not completely support all scenarios, so WinAppSDK just prevents them from happening (i.e. no elevation).
As a drive-by note, I bet that this thread and #3816 could both be duped to #3595, and just turn #3595 into the feature request thread.
In #137 it was mentioned that notifications from admin apps should just work - I'm not sure if that ever actually was supported, or regressed recently, or what exactly. /cc @andrewleader for confirmation.
@KWodarczyk Please check the limitations section here: https://learn.microsoft.com/en-gb/windows/apps/windows-app-sdk/notifications/push-notifications/ and https://learn.microsoft.com/en-gb/windows/apps/windows-app-sdk/notifications/app-notifications/ Notifications for an elevated (admin) app are currently not supported.
Oh that is so sad, do you how when it will be supported? How come elevation for apps is supported but this feature is not ? What can be done for elevated apps ?