Windows.Ui.Notifications Is Missing

Windows.UI.Notifications is missing

You have to fight Visual Studio pretty hard to use these UWP contracts in a Winforms app. You got off on the wrong foot right away with the wrong TargetPlatformVersion, pretty hard to recover from that. Full steps to take:

Edit the .csproj file with a text editor, Notepad will do. Insert this:

  <PropertyGroup>
<TargetPlatformVersion>10.0.10586</TargetPlatformVersion>
</PropertyGroup>

Which assumes you have the 10586 SDK version installed on your machine. Current right now, these versions change quickly. Double-check by looking in the C:\Program Files (x86)\Windows Kits\10\Include with Explorer, you see the installed versions listed in that directory.

Open the Winforms project, use Project > Add Reference > Windows tab > tick the Windows.Data and the Windows.UI contract. Add Reference again and use the Browse tab to select System.Runtime. I picked the one in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\ .NETFramework\v4.6.1\Facades. This reference displays with a warning icon, not sure what it is trying to say but it doesn't appear to have any side-effects.

Test it by dropping a button on the form, double-click to add the Click event handler. The most basic code:

using Windows.UI.Notifications;
...

private void button1_Click(object sender, EventArgs e) {
var xml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01);
var text = xml.GetElementsByTagName("text");
text[0].AppendChild(xml.CreateTextNode("Hello world"));
var toast = new ToastNotification(xml);
ToastNotificationManager.CreateToastNotifier("anythinggoeshere").Show(toast);
}

Embellish by using a different ToastTemplateType to add an image or more lines of text. Do keep in mind that your program can only work on a Win10 machine.

Toast Notifications are not displaying outside IDE debugger

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}"/>

Add notification to Windows notification center without displaying it on screen

With the help of @treckstar in comments, I have found a way to do what I wanted using:

  • NodeRT
  • The ToastNotification.SuppressPopup attribute

Despite a handful of troubles building NodeRT and using electron-rebuild, here's a working PoC:

const { XmlDocument } = require('@nodert-win10-rs4/windows.data.xml.dom');
const {
ToastNotification,
ToastNotificationManager
} = require('@nodert-win10-rs4/windows.ui.notifications');

const localImage = path.join(__dirname, 'icon.png');
const template = `
<toast launch="app-defined-string">
<visual>
<binding template="ToastGeneric">
<image id="1" placement="appLogoOverride" hint-crop="circle" src="${localImage}"/>
</binding>
</visual>
</toast>
`;

const xml = new XmlDocument();
xml.loadXml(template);

const toast = new ToastNotification(xml);
const notifier = ToastNotificationManager.createToastNotifier("com.myapp.testnotif");
toast.suppressPopup = true;
notifier.show(toast);

May this help whoever comes across the same highly-specific problem.

How can I use the Windows.UI namespace from a regular (Non-Store) Win32 .NET application?

Certain WinRT classes can be used from desktop apps, including portions of the Windows.UI namespace. The toast notification APIs are one such example - they can be used by both Windows Store apps and desktop apps. In the API reference section, each class page shows if the API applies to Windows Store apps or desktop apps (example for toast). There is a sample of using the toast notification APIs from a desktop app provided on MSDN.

MSDN contains a full list of the WinRT classes that are available to desktop apps.

Edit:

The ability to add a reference to the WinRT APIs is off by default in C# desktop apps. The project needs to specifically target the Windows 8 platform. To add a reference to WinRT APIs (the Windows.winmd assembly), add the following to your project file:

<PropertyGroup>
<TargetPlatformVersion>8.0</TargetPlatformVersion>
</PropertyGroup>

After adding that to the project, a "Windows" tab will appear in the Reference Manager dialog, and you can add the Windows assembly. For more information, see 'Core Subgroup' on this MSDN article.

Toast notification not working on Windows fall creators update

As mentioned in the comments, this is something that recently had to be addressed in the BurntToast module. There's a blog post that accompanies this change too, but I'll do my best to summarize here for the completeness of this answer.

This boils down to the "Application User Model ID" (hereafter referred to as AppId), that you're providing to the Toast Notification Manager.

Strictly speaking, this AppId needs to match a an AppId embeded in a shortcut that's sitting in your Start Menu. This has always been the case, however there was a loophole of sorts that allowed any old AppId in previous versions of Windows 10.

As much as it sucks for those of us who are creating Toasts from scripts, that loophole has been closed and our AppIds, as of the Fall Creators Update, need to be "real."

You can find a list of valid AppIds by running:

Get-StartApps

I've opted to default to the one for PowerShell.exe:

{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe

It should be noted that you still need to configure some of these (including PowerShell) so that their Toasts are actually displayed in the Action Center when they time out.

You can do this via a "Settings":

Settings -> System -> Notifications & actions -> PowerShell (scroll down, you'll have needed to have sent at least one Toast for it to appear) -> Show notifications in action center.

PowerShell notification settings

You can also do this via the registry, under HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings

For the PowerShell example, you would add a DWORD (set to 1) called ShowInActionCenter under:

HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe\

If you want to do your own AppId, you'll need to look at how to create a shortcut with an AppId, or creating a dummy UWP app via an AppxManifest.xml. I'm still working on a user friendly way of doing one of these.



Related Topics



Leave a reply



Submit