Notifications in Windows 8: how to display new-style toast from a desktop app

This post was prompted by the discussion over the fact that Windows 8 boots into the Start screen, and cannot apparently be modified to boot into the Desktop (though no doubt someone will find a way).

What if, I wondered, you put a desktop app into your startup folder so it runs automatically on boot? The answer: it runs on the desktop, but you will not see it until you click or tap into the desktop from the start screen. This is different behaviour from actually starting a desktop app from the start screen, which switches you to the desktop.

So what if that desktop app has something important to tell you? The answer: you will not see it until you switch to the desktop.

To demonstrate this, I wrote a Windows Forms app that displays a MessageBox alert after a 5 second delay. I ran the app, activated the alert, and switched to a Windows Runtime Metro app. When the alert fired, I heard a little ding, but saw no message. Only after switching to the desktop did I see the message.

image

To be fair, you might not see this even if you were working in the desktop, since Windows has complex (and sometimes unpredictable) rules about when apps are allowed to come to the foreground. Even calling the Activate method, which gives your window the focus, may do no more than flash the icon on the taskbar.

Windows 8 has a new-style “toast” notification mechanism that works across both desktop and Windows runtime. I got this working in my Windows Forms app.

image

So how do you do this? For some background, see Jim O’Neil’s series of posts which start here. However, I mostly used code from the sample Sending toast notifications from desktop apps. This is a WPF application, but I got the code to work in my Windows Forms application. Note that to reference Windows.UI.Notifications you have to add a reference to:

C:\Program Files (x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral\Windows.winmd

Update: in the release version of Visual Studio 2012 the way you reference winmd has changed. See this MSDN article and the section called Core Subgroup; however at the time of writing the method described there does not quite work. Instead, proceed as follows. First right-click the project in the Solution Explorer and choose Unload Project. Then right-click the project again and choose Edit [project].csproj. Add the following to the project file after the other Reference elements:

<Reference Include="Windows" />

then save and close the editor. Finally, right-click the project name again and choose Reload project. Do not add the suggested TargetPlatformVersion element, since if you do the project will not compile.

You also need the Windows 7 API code pack which is here.

Here is a quick summary though. In order to display a toast notification, you first need a shortcut to your app on the Start menu. In addition, the shortcut has to have an AppUserModeId, which you can set in code.

Once that is sorted, you can use the ToastNotificationManager class – for which you need that reference to Windows.UI.Notifications – and retrieve a standard XML template for the notification. You can add event handlers to the notification, so you can respond if the user clicks it. Then call:

ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);

to actually show the notification.

Note that your event handlers will not fire on the UI thread, so you need to use thread-safe methods if you want to interact with controls on your form.

It is all somewhat laborious, but on the plus side you get notifications which are better than the old notification area toast, and much better than MessageBox.

Note that this is one bit of code applications will only have if they are modified to work on Windows 8. That is worth noting if you have an application in which notifications play an important role.

image

VN:F [1.9.18_1163]
Rate this post
Rating: 10.0/10 (4 votes cast)
Notifications in Windows 8: how to display new-style toast from a desktop app, 10.0 out of 10 based on 4 ratings

Related posts:

  1. Windows on ARM: Microsoft can write Desktop apps, but you cannot
  2. Blue screen, Windows 8 style
  3. Using Windows Runtime (WinRT) APIs from desktop applications
  4. Fixing a Metro-style app in Windows 8 Consumer Preview
  5. Miguel de Icaza talks about Windows 8 and the failure of Linux on the desktop

5 comments to Notifications in Windows 8: how to display new-style toast from a desktop app

  • Tom

    It’s not that you won’t see the desktop app until you switch to the Desktop. The desktop apps in the Startup folder won’t run at all until you launch the Desktop tile.

    This design allows for faster startup and lower memory usage — so long as you stay within the Metro environment. The moment you launch the Desktop, all the Startup apps will run, and remain running for the rest of the user session.

  • Link

    Tom, that is just not true. If you put something in the startup folder, it WILL start at startup.

    Thanks for this post; I’ve been wanting to know how to create toast notifications. I’m sick of using growlforwindows.

    Here’s another surefire way to get a popup noticed; have it trigger a UAC prompt and name the executable the message you intend to deliver! Admittedly I have seen the prompt remain minimized, there’s some arbitrary rule about it not popping up depending on what you’re doing, but apparently it has no problem popping up when I’m in the middle of typing something… 8 Notifications are really the best solution for this, being unified is a plus too. I once had a chat program that brought focus to the input box when the notification went off, but the window was still hidden until you clicked the notification. I sent out my password while trying to log into a site several times.

  • tim

    Something in the startup folder starts when you access the desktop for the first time, not before. You could in theory have a whole session without ever accessing the desktop, in which case it would never run.

    Tim

  • Link

    I’m sorry, you just don’t know what you’re talking about. Go try it for yourself. I just verified it by putting quake2 in the startup folder. Even in metro/ModernUI, without ever having “started the desktop” the mouse was locked in place because, you guessed it, things in the startup folder start when the computer starts up.

    Quake2 was prevented from making sound (oddly) and it failed to go fullscreen as it was a window when I entered the desktop, but it was definitely running and my GPU was definitely rendering it behind the start screen (GPU fan kicked on).

    I’m running 8 Pro RTM x64 w/ Media Center, build 9200, but I think even the Developer Preview operates the same, in that they did not change the way this functions.

  • tim

    Hi

    I checked and you are right, commands run though the GUI does not show. I’ll correct it.

    Thanks

    Tim