Who we are

We are the developers of Plastic SCM, a full version control stack (not a Git variant). We work on the strongest branching and merging you can find, and a core that doesn't cringe with huge binaries and repos. We also develop the GUIs, mergetools and everything needed to give you the full version control stack.

If you want to give it a try, download it from here.

We also code SemanticMerge, and the gmaster Git client.

How to send Windows Toast notifications from Console apps

Wednesday, August 17, 2016 Pablo Santos 1 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.

Motivation

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: https://github.com/psantosl/ConsoleToast/commit/8b20c596bfef00040869bbb6e6cb6bbb249a5816. 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:

Conclusion

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 :-)

Enjoy.

Pablo Santos
I'm the CTO and Founder at Códice.
I've been leading Plastic SCM since 2005. My passion is helping teams work better through version control.
I had the opportunity to see teams from many different industries at work while I helped them improving their version control practices.
I really enjoy teaching (I've been a University professor for 6+ years) and sharing my experience in talks and articles.
And I love simple code. You can reach me at @psluaces.

1 comment:

  1. I used this thread to help make this code - I am sharing my success.

    *warning I am kind of new to C# so my code probably sucks- but it does work and is pretty simplistic and that's more than I can say for most solutions I have found*

    Also I was having a hell of a time getting the xml document to read. I was fighting with System.xml (I think) and Windows.Data.Dom.Xml (also not completely sure).
    In the end I settled on making them hard coded strings for my example file and used a switch statement to switch between them.
    I have found a ton of people, looking for the solution that I have come up with, on stack overflow. It seems use of the toast notification system with console or background applications would be super useful, and the documentation that surrounds the toast notification system with windows applications all suggest that it needs to be used with an application. The Action Center is super useful for notifications vrs the NotificationTray/NotifyIcon route. I have not found a full solution anywhere else on the web. Here is example code.

    /*
    At first you need to declare that your program will be using winRT libraries:
    1. Right click on your yourProject, select Unload Project
    2. Right click on your youProject(unavailable) and click Edit yourProject.csproj
    3. Add a new property group:8.0
    4. Reload project
    5. Add referece Windows from Windows > Core
    */
    using System;
    using Windows.Data.Xml.Dom;
    using Windows.Storage;
    using Windows.Storage.Streams;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.UI.Notifications;

    namespace ConsoleApplication6
    {
    public class NewToastNotification
    {
    public NewToastNotification(string input, int type)
    {
    string NotificationTextThing = input;
    string Toast = "";
    switch (type)
    {
    case 1:
    {
    //Basic Toast
    Toast = "";
    Toast += NotificationTextThing;
    Toast += "";
    break;
    }
    default:
    {
    Toast = "";
    Toast += "Default Text String";
    Toast += "";
    break;
    }
    }
    XmlDocument tileXml = new XmlDocument();
    tileXml.LoadXml(Toast);
    var toast = new ToastNotification(tileXml);
    ToastNotificationManager.CreateToastNotifier("New Toast Thing").Show(toast);
    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    NewToastNotification Window = new NewToastNotification("Yes",1);


    }
    }
    }

    ReplyDelete