How to display a toast notification without adding it to the action center

How to display a toast notification without adding it to the action center

One of the current restriction of UWP is that we don’t have a simple way to display toast notifications without adding them to the action center.

It can make sense to keep visible some toasts during days there, for example: “Sabrina sent you a message”, is the kind of toast you don’t want to miss and still valid even after 2-3 days. But it doesn’t make sense to keep some toasts more than 20 seconds, for example “the password you entered is wrong”, “Your message has been sent”, etc… I named this type of toasts “action feedback toasts”, their only purpose is to show a UI feedback following the result of an action you did: click on the send button, check a radio button, etc… Those toasts are only interesting in the very short term, they should not even added to the action center in my opinion.

Do we have an API to prevent toasts to be added to the action center?

No. There is an API add toasts to the action center without popping them (ToastNotification.SuppressPopup), but the contrary doesn’t exist, we will need to find a workaround.

The classic, but wrong, solution

A very common solution we find on internet is to use ToastNotification.ExpirationTime and assign the current time + 10 seconds.

It works, the toast will automatically disappear after 10 seconds, but in some case, your toast will never be popped. For example, if an app (yours or an other) is already displaying a toast, in this case, your toast will be added to the pending queue, but will probably expire before being able to be displayed (It’s even worse when a reminder is displayed)

What’s the best solution?

To be honest, the best solution is… to not use ToastNotification for “action feedback toasts”, ToastNotifications are not reliable, for example they can be disabled by the user if “Don’t disturb” mode is activated.

Instead, you should build your own toasts and host them directly in your XAML pages, so even you will be sure to always display them.

But for any reasons, if you need it, you should not use ExpirationTime but instead use ToastNotification events and detect when your toasts are no more visible to remove them manually after.

Let’s do a list of every case:

  • ToastNotification.Failed called
    • but we have nothing to do cause the toast has not be added to the action center
  • ToastNotification.Activated called
    • The user clicked on the toast or a button in the toast
    • Nothing to do cause the toast has been removed from the action center after the action
  • ToastNotification.Dismissed called
    • args.Reason == ToastDismissalReason.ApplicationHidden
      • The application called .Hide()
        • Nothing to do, the toast is already hidden
    • args.Reason == ToastDismissalReason.UserCanceled
      • The user clicked on close or swiped the toast
        • The toast is still in the action center, we should remove it manually
    • args.Reason == ToastDismissalReason.Timeout
      • The toast disappeared automatically after 6-7 seconds
        • The toast is still in the action center, we should remove it manually

If we summarize, we have only 2 case to manage: when Dismissed is called with UserCanceled or Timeout.

So here is the code you should use if you want your toasts to not stay in the action center once displayed:

 

ToastNotification toast;
toast.Dismissed += Toast_Dismissed;
...

private void Toast_Dismissed(ToastNotification sender, ToastDismissedEventArgs args)
{
     if (args.Reason == ToastDismissalReason.UserCanceled || args.Reason == ToastDismissalReason.TimedOut)
     {
          ToastNotificationManager.CreateToastNotifier().Hide(sender);
     }
}
Comments are closed.