{"id":6217,"date":"2012-08-09T13:07:31","date_gmt":"2012-08-09T12:07:31","guid":{"rendered":"http:\/\/www.itwriting.com\/blog\/?p=6217"},"modified":"2012-08-09T13:07:31","modified_gmt":"2012-08-09T12:07:31","slug":"notifications-in-windows-8-how-to-display-new-style-toast-from-a-desktop-app","status":"publish","type":"post","link":"https:\/\/www.itwriting.com\/blog\/6217-notifications-in-windows-8-how-to-display-new-style-toast-from-a-desktop-app.html","title":{"rendered":"Notifications in Windows 8: how to display new-style toast from a desktop app"},"content":{"rendered":"<p>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).<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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 <strike>Metro<\/strike> 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.<\/p>\n<p><a href=\"http:\/\/www.itwriting.com\/blog\/wp-content\/uploads\/2012\/08\/image31.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.itwriting.com\/blog\/wp-content\/uploads\/2012\/08\/image3_thumb.png\" width=\"404\" height=\"294\" \/><\/a><\/p>\n<p>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.<\/p>\n<p>Windows 8 has a new-style \u201ctoast\u201d notification mechanism that works across both desktop and Windows runtime. I got this working in my Windows Forms app.<\/p>\n<p><a href=\"http:\/\/www.itwriting.com\/blog\/wp-content\/uploads\/2012\/08\/image11.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.itwriting.com\/blog\/wp-content\/uploads\/2012\/08\/image1_thumb.png\" width=\"404\" height=\"264\" \/><\/a><\/p>\n<p>So how do you do this? For some background, see Jim O\u2019Neil\u2019s series of posts which start <a href=\"http:\/\/blogs.msdn.com\/b\/jimoneil\/archive\/2012\/08\/02\/windows-8-notifications-overview.aspx\" target=\"_blank\">here<\/a>. However, I mostly used code from the sample <a href=\"http:\/\/code.msdn.microsoft.com\/windowsdesktop\/Sending-toast-notifications-71e230a2\/sourcecode?fileId=51047&amp;pathId=611218636\" target=\"_blank\">Sending toast notifications from desktop apps<\/a>. This is a WPF application, but I got the code to work in my Windows Forms application. <strike>Note that to reference Windows.UI.Notifications you have to add a reference to:<\/strike><\/p>\n<blockquote>\n<p><strike>C:\\Program Files (x86)\\Windows Kits\\8.0\\References\\CommonConfiguration\\Neutral\\Windows.winmd<\/strike><\/p>\n<\/blockquote>\n<p><strong>Update<\/strong>: in the release version of Visual Studio 2012 the way you reference winmd has changed. See <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/hh708954(v=vs.110).aspx\" target=\"_blank\">this MSDN article<\/a> 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:<\/p>\n<blockquote>\n<p>&lt;Reference Include=&quot;Windows&quot; \/&gt;<\/p>\n<\/blockquote>\n<p>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.<\/p>\n<p>You also need the Windows 7 API code pack which is <a href=\"http:\/\/archive.msdn.microsoft.com\/WindowsAPICodePack\/Release\/ProjectReleases.aspx?ReleaseId=4906\" target=\"_blank\">here<\/a>.<\/p>\n<p>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.<\/p>\n<p>Once that is sorted, you can use the ToastNotificationManager class &#8211; for which you need that reference to Windows.UI.Notifications \u2013 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:<\/p>\n<blockquote>\n<p>ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);<\/p>\n<\/blockquote>\n<p>to actually show the notification.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p><a href=\"http:\/\/www.itwriting.com\/blog\/wp-content\/uploads\/2012\/08\/image8.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px\" border=\"0\" alt=\"image\" src=\"http:\/\/www.itwriting.com\/blog\/wp-content\/uploads\/2012\/08\/image_thumb8.png\" width=\"404\" height=\"289\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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? &hellip; <a href=\"https:\/\/www.itwriting.com\/blog\/6217-notifications-in-windows-8-how-to-display-new-style-toast-from-a-desktop-app.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Notifications in Windows 8: how to display new-style toast from a desktop app<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,67,80,97],"tags":[645,999],"class_list":["post-6217","post","type-post","status-publish","format-standard","hentry","category-net","category-professional","category-software-development","category-windows","tag-notifications","tag-windows-8"],"_links":{"self":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/6217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/comments?post=6217"}],"version-history":[{"count":0,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/6217\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/media?parent=6217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/categories?post=6217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/tags?post=6217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}