Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 10 Notification WPF

I'm trying to figure out if it is possible through my WPF application, to access the built in notification service that exists in Windows 10. I'm using VS 2015 and c#. Also, is the toasternotification the same thing? They dont look like that anymore in Windows 10. If yes, Could you please guide me in the right direction to namespace etc?

Yes, I have searched the web and only found toasternotification for Win 7. And that is not what I'm looking for.

like image 897
Filip Jönsson Avatar asked Mar 10 '16 07:03

Filip Jönsson


2 Answers

Found a code sample that is similar to what you need, but only does Toast Notifications.

You basically want to have a regular .NET application that references the Windows.UI components.

To use the Windows 10 Notifications you need to edit your csproj file and add the target platform,

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<TargetPlatformVersion>8.1</TargetPlatformVersion>
</PropertyGroup>

Once you do this, you should be able to add a reference to the Windows.UI assemblies.

Right click the References node, and click on Windows on the left side pane. Select the checkbox for Windows.UI, Windows.Data and Windows.Foundation.

Next on your form class file, add using Windows.UI.Notifications; to access the ToastManager component.

Once you have that, access the template you want to use

 // Get a toast XML template
 var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText02);

 // Fill in the text elements
 var stringElements = toastXml.GetElementsByTagName("text");
 stringElements[0].AppendChild(toastXml.CreateTextNode("Title"));
 stringElements[1].AppendChild(toastXml.CreateTextNode("Content"));

Here are the different Toast type enumerations.

Once you have a reference to the Toast template you have to create a ToastNotification and send it to the ToastNotificationManager

 // Create the toast and attach event listeners
 var toast = new ToastNotification(toastXml);
 toast.Activated += ToastActivated;
 toast.Dismissed += ToastDismissed;
 toast.Failed += ToastFailed;

 // Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
 ToastNotificationManager.CreateToastNotifier("My Toast").Show(toast);

You can attach events for the Activated, Dismissed and Failed event handlers too.

like image 95
Leo Hinojosa Avatar answered Nov 02 '22 01:11

Leo Hinojosa


For future reference:

Here is described very well how to use new windows 10 toast notifications. And you don't need to install your app beforehand or set AppUserModelID. Works directly from Visual Studio (I mean debug mode) and simplest code looks like this:

new ToastContentBuilder()
    .AddText("Something copied to clipboard")
    .Show();

But don't forget to change your target platform in your *.csproj file (everything is described in the article).

<TargetFramework>net5.0-windows10.0.17763.0</TargetFramework>
like image 32
Dmitriy Bobrovskiy Avatar answered Nov 02 '22 01:11

Dmitriy Bobrovskiy