How to Show Toast Notifications in WinForms Apps

WinForms Team Blog
10 February 2020

Toast Notifications (or simply Toasts) are flat notifications first introduced in Microsoft Windows 8. They pop up in the bottom right corner of your screen and can be accessed through the Windows Action Center. You can incorporate these notifications in your next Windows Forms app by using the DevExpress ToastNotificationManager component. To see toasts in action, launch our "Outlook Inspired App" demo and wait for it to load its notifications.

Our WinForms Toast Notification Manager offers nine notification templates/styles - including a Windows 10 "Generic" template that allows you to incorporate user interaction controls such as buttons or editors.

Application Shortcut

The DevExpress WinForms Toast Notification Manager generates genuine Windows toasts. These toasts are just like system notifications:

  • Toasts appear even if the associated application is closed.
  • Toasts can be accessed from the Action Center.
  • Toasts use the same color scheme as the operating system.

To deliver this capability, we had to use native Windows APIs and follow all Microsoft guidelines and requirements. This included Microsoft’s application shortcut requirement.

Unfortunately, Microsoft’s application shortcut requirement has caused some confusion in the past. We recently made a minor design-time improvement and added a new section to the component's documentation page to help address this issue going forward.

In its Toast Notifications Overview Microsoft explicitly states that if an app does not have a shortcut in the Windows Start screen, it cannot display toasts.

Since Start screen shortcuts are stored in the %AppData%\Microsoft\Windows\Start Menu\Programs folder, we need to follow a simple rule: To display toasts, add an application shortcut to Start Menu folder.

Our component includes a "Create Application Shortcut" link in its smart tag menu. Once you click this link, your app will correctly display notifications.

As some of you have discovered, our "Create Application Shortcut" command adds a shortcut to one’s personal Start screen. Said differently, the PC that created the application shortcut is the only machine able to display toasts… No other machine’s Start screen has a shortcut to your app. If you want to display toasts for all users (and not just yourself), remember to configure your application installer to add a shortcut (with a valid AppUserModelID) in the %AppData%\Microsoft\Windows\Start Menu\Programs folder.

We believe that your best option is to use an installer to add this shortcut to user machines. If this is not an option or if you don’t have an installer, you can use the DevExpress.Data.ShellHelper.TryCreateShortcut method to add the appropriate shortcut.

using DevExpress.XtraBars.ToastNotifications;
using DevExpress.Data;

//if there's no app shortcut in the start screen, add it
if (!ShellHelper.IsApplicationShortcutExist("My Test App")) {
    ShellHelper.TryCreateShortcut(
        applicationId: manager.ApplicationId,
        name: "My Test App");
    //restart the app
    Application.Restart();
}

Please note that the TryCreateShortcut method is less reliable than the alternative. First and most important - your app may not have the necessary permissions to write files into system folders. Second, note the use of the Application.Restart method in the code snippet above. If the application is already running, adding its shortcut to the Start screen will not enable toasts. Application restart is required.

Activator and COM Server

Windows retains toast notifications in its Action Center.

Normally, Action Center clears all toasts after a user closes this screen (if a user opens, closes, and opens the Action Center again, it will be empty). Some toasts, however, can remain in the Action Center until a user manually dismisses them. Such toasts remain in the Action Center even after a user logs out and back into Windows. Additionally, these toasts launch their parent applications when clicked. To display such notifications, create a custom Activator - a descendant of the DevExpress.XtraBars.ToastNotifications.ToastNotificationActivator class.

[Guid("39697E4E-3543-4414-A694-90097B433DC6"), ComVisible(true)]
public class ToastNotificationActivatorCustom : ToastNotificationActivator {

    public override void OnActivate(string arguments, Dictionary<string, string> data){
        //specify what happens when a user interacts with a toast
    }
}

You will need to register this custom Activator. Set the component's ApplicationActivator property at design time or call the RegisterApplicationActivator method in code.

toastNotificationsManager1.RegisterApplicationActivator(
		typeof(ToastNotificationActivatorCustom));

You will also need to register a COM Server. To do so, call the DevExpress.Data.ShellHelper.RegisterComServer method. Note that if you use the ShellHelper class to create an application shortcut, you need to use a TryCreateShortcut method overload that takes the activatorType parameter. Otherwise, if you use the application installer to add the application shortcut, the installer must also add the HKEY_CURRENT_USER\SOFTWARE\Classes\CLSID\{-your-GUID-here-}\LocalServer32 registry key with a path to application executable file as a value.

if (!ShellHelper.IsApplicationShortcutExist("My Toast Application")
{
    ShellHelper.TryCreateShortcut(
         Process.GetCurrentProcess().MainModule.FileName,
         toastNotificationsManager1.ApplicationId,
         "My Toast Application",
         @"D:\Work\Images\_Icons\ico\chain-icon.ico",
         typeof(ToastNotificationActivatorCustom));
    ShellHelper.RegisterComServer(
        Process.GetCurrentProcess().MainModule.FileName,
        typeof(ToastNotificationActivatorCustom));
}

GUIDs

When working with toasts, you will need several unique IDs.

  • For the component's ApplicationID property
  • For the ID property of every notification
  • For the Guid attribute that labels an Activator class

You can mash a keyboard to generate a unique ID, but a more effective approach is to use an online GUID generator (such as this one) or the generator included in Visual Studio ("Tools | Create GUID").

Deliver Critical Notifications

Even if you set up everything correctly, a notification may not delivered to a user. Delivery can be affected by:

  • an older OS version
  • notifications disabled in Action Center settings
  • internal errors, etc.

In such circumstances, you can handle the ToastNotificationsManager.Failed event to deliver critical messages to users via alternative methods (such as a standard message box).

using DevExpress.XtraBars.ToastNotifications;
using DevExpress.XtraEditors;

void manager_Failed(object sender, ToastNotificationFailedEventArgs e)
{
    if ((string)e.NotificationID == "toast_connection_lost_id_das0ud0q94")
    {
        IToastNotificationProperties undeliveredToast =
            toastNotificationsManager1.GetNotificationByID(e.NotificationID);
        XtraMessageBox.Show(undeliveredToast.Body, undeliveredToast.Header);
    }
}

Should you have any questions about Toast Notifications or you encounter issues with implementation, please post comments below. We’ll be happy to follow up.

FREE DevExpress Xamarin UI Controls

Deliver high-impact native mobile apps with our feature-rich Data Grid, Scheduler, Chart, TabView, Editors, and utility controls. Reserve your free copy today!
Christopher Jay
Christopher Jay
This is nice.  I like the Failed event so I can handle the old OSes and fallback to our old toasts.  Looking forward to implementing soon.  Thanks!
10 February 2020
DANIEL SASS
DANIEL SASS
Is there/will there be an equivalent in WPF?
11 February 2020
Alex Chuev (DevExpress)
Alex Chuev (DevExpress)

Daniel,


You can display toast notifications in WPF apps using the NotificationService class from our MVVM Framework.


Thanks,

Alex

11 February 2020
Benjamin Hofmann
Benjamin Hofmann
Great article!
11 February 2020
Garry Lowther
Garry Lowther
Nice article. I'm looking forward to cracking on with this today.
2 July 2020
CRM-f3d3f937-ca73-4317-8298-e068588e93c9
Customer177131
You can use gb whatsapp at <a href=https://androidrub.com/gb-whatsapp/>Cliquez ici</a> to receive daily notifications.
25 November 2020
CRM-f3d3f937-ca73-4317-8298-e068588e93c9
Customer177131
You can use gb whatsapp at https://androidrub.com/gb-whatsapp Cliquez ici to receive daily notifications.
25 November 2020
CRM-d3a973f0-41a0-408c-995f-dac269e152cf
Customer179634

Nice, yes this is working for me.

<a rel="nofollow" href="https://premiumapkspot.com/dragon-city-game/"></a>

13 December 2020
CRM-60285cc2-8bf5-4497-ad7f-9706627cb316
Customer180352
You can use <a href="https://gbapps.info/gacha-life/">Gacha Life</a> to create the best character and use it in your profile creation or playing the games.
17 December 2020
CRM-60285cc2-8bf5-4497-ad7f-9706627cb316
Customer180352
You can also see this https://gbapps.info/gacha-life/
17 December 2020
CRM-b7c0f4e5-cad6-4e8a-959b-2f506aa7c754
Customer181291
Unfortunately, Microsoft’s application shortcut requirement has caused some confusion in the past.
https://goreadreviews.com
27 December 2020
CRM-b07aadce-24e3-4917-8a53-3e5d44c06d7e
Customer183726
Thanks for sharing!!!. Also see this https://thetechbytes.net/
14 January 2021
CRM-b07aadce-24e3-4917-8a53-3e5d44c06d7e
Customer183726
Thanks for sharing that [post](https://thetechbytes.net/).
14 January 2021
CRM-7539edc6-ab1a-4a02-97c7-7452ec7753ae
Customer189072

Hey, thank you a lot for sharing this article with us. I can’t say, how grateful we are to read this. Also, I would love to share it with my friends and family, who are interested. I hope you will publish such articles in the future as well. It’s so helpful. Goodbye!  Take a look at this wonderful article, https://clashmod.net/rebrawl-brawl-stars-mod-download/ 

19 February 2021
CRM-77925d13-142d-40fe-a146-b62f44376ac4
Customer191546
An oven teapot is most ordinarily utilized over the house yet with the progression of time innovation presents an electric teapot that works quickly and proficiently. https://kettlesboiler.com
6 March 2021
CRM-77925d13-142d-40fe-a146-b62f44376ac4
Customer191546
​oscar awards live streaming you can watch complete detail and live streaming updates about the oscar awards. oscarsaward2021.com
7 March 2021
CRM-f6bfe515-71b9-4f3b-b117-8a9657134683
Customer195064

Have you ever been in a situation in a game where you are about to shoot at your enemy, and suddenly your mouse cable gets tangled to your drawer handle, and you miss your aim by 1 millimeter?

Well, this is a common cause for many gamers out there. And while cordless mice might seem like a viable option, those come at the cost of delayed input.

https://best-mouse.com/best-mouse-bungee/

So what’s the alternative? You can buy a mouse bungee, of course! These products let you have control over your mouse while reducing all drag. Confused? Read our article about the best mouse bungees and look at our guidelines, and you will have a better understanding in no time!

<a href="https://best-mouse.com/best-mouse-bungee/" style="color:#0073aa;">10 Best Mouse Bungee 2021 Reviews & Buying Guide</a>



27 March 2021
CRM-7c99e861-790f-4a03-8020-7b6e363556c5
Customer202470
Thanks for sharing this article. This is very helpful to me!!! also see <a href="https://thetechbytes.net" style="color:#0073aa;">thetechbytes.net</a>
12 May 2021
CRM-7c99e861-790f-4a03-8020-7b6e363556c5
Customer202470
Nice post. Thanks for sharing. <a href="https://thetechbytes.net">thetechbytes.net</a>
12 May 2021
CRM-a8037a84-a9b0-46a5-bf5e-588239af2c3f
Customer204227
Best blog and community ever Thanks to everyone. https://iangelnumbers.com
23 May 2021

Please login or register to post comments.