How to send Windows Toast notifications from Console apps

Wednesday, August 17, 2016 0 Comments

Toast notifications are these small panels showing up on the right side of the screen, displaying useful tips about running apps.

Creating Toasts notification is straightforward if you are coding Universal apps, but it takes a little bit more when you are on desktop or console apps.

This blogpost shows how to create Toast notifications from a C# console application. The code is available on a GitHub repo (pushed from Plastic, btw) and it runs both on W10 and W8.x.


Why would you need to show a Toast from a command line app? Well, in my case I wanted to add visual feedback when certain version control triggers completed. Like you change something on your JIRA when you checkin to a certain branch, but you want to be sure the trigger completed.

In my case I was building a “geolocated checkin trigger” to record where every checking was made (GPS-wise).

Toasts from Desktop apps

There is a docu explaining how to do Toasts from Desktop. So, all I did was to apply the same ideas to my console app, which is not difficult but not as obvious as it should either.

Creating a clean Console C# app able to launch Toast Notifications

It is all about being careful with the references to add. It could be simpler, but instead of just selecting some system assemblies as you would normally do, to use Windows Runtime from Desktop or Console, you have to perform some manual steps (well defined here). So I will go step by step, explaining what I did:

  • Edit your .csproj to enable the Windows Runtime on the Console app (not enabled by default, and not doable trying to add references). Check the exact commit where I did it on mine: It is just about adding 8.0 to your csproj, that's all, but you have to do it manually outside Visual Studio.
  • Add all the new references:
    • Add a reference to Windows.winmd. You have to do it manually, so first you should find where your file is. In my case: C:\Program Files (x86)\Windows Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd
    • I also added WindowsBase.dll doing the same thing, looking for the dll directly on my disk: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\WindowsBase.dll
    • And repeat the same for System.Runtime.InteropServices.WindowsRuntime.dll, which didn't show up as a “system assembly” either.
    • And finally System.Runtime.dll.
    • The commit including these changes is 81f8
  • Import the right NuGets: WindowsAPICodePack-Core, WindowsAPICodePack-Shell and WindowsAPICodePack-ShellExtensions. You can do this easily from the “Tools/NuGet Package Manager” menu.

While you can see the references in the GitHub repo, the following screenshot also shows the ones I used:

And the screenshot of the NuGets to install:

As I said above, this checkin shows the references I setup on my project.

Sending notifications

After the previous reference configuration, we can focus on launching Toasts.

Sending a Toast is super simple, it is just a matter of creating a Windows.UI.Notifications.ToastNotification and invoking ToastNotificationManager.CreateToastNotifier(appId).Show(toast).

But there is some plumbing to do before:

  • You need your app to be installed in the “start up” menu. Yes, only apps installed this way are allowed to send Toasts.
  • To do that, you need some code (copied from MS sample in my case) to create a shortcut with an AppID (which is just a name you give to your app).

And then you are done, ready to send beautifully formatted Toasts.

There are some different templates to create Toasts, all obtained invoking ToastNotificationManager.GetTemplateContent. In my code I created just 2: one plain text and one including an image.

It runs on Win8 too

I just tested that the compiled binaries run on my W10 and also on a W8.1 VM. In case you don't believe me, check the following picture:


My code doesn't add much to the original example by Microsoft, explaining how to do Toasts from a Desktop App. But for some reason it is what I expected to find when I wanted to learn about this type of notifications. Nowadays it seems each time you read an example of a new MS API, you have to delve with a bunch of WPF code here and there, showing off how to create some dialogs and stuff, instead of focusing on the real thing being done (which normally ends up being like 1% of the total code in the example). That's why I wanted to share my Console code to display Toasts :-)


0 comentarios: