Action Center Toast taming on Windows Phone 8.1

Toast notifications is a powerful tool for sending transient message to the user. The can be invoked from an app, a background agent or from any service on the web through push notifications.

Starting with Windows Phone 8.1, there’s an action center that will show all kinds of notifications – including toasts. This post will show you how to control how your toast messages behave in the action center. You can make them either not show up at all in the action center, or disappear after a period of time.

Sending toasts

First and foremost, don’t forget to make your app toast-capable in the app manifest! Easy to forget.

image

The format of toast and tile notifications has, starting with 8.1, been unified with Windows and is based on an Xml format.

Note! On Windows, you can use any notification type from the ToastTemplateType enumeration, but on Windows Phone all notifications will be rendered as a variant of toastText02, with one bold text and one normal text side by side.

The ToastText02 Xml looks like this:

And to create a toast, the C# is:

Toast:

1 toast from jay

Toast in action center:

2 toast from jay in AC

Controlling timeout

There is an expiration time property on the toast that can be used for controlling Action Center behaviour. Feels like the primary use case for this is when sending notifications from a server – then you really want to control when a toast is valid to show and when its not. But, it can be used locally as well!

Setting the ExpirationTime is simple:

Skipping the property will make the toast stay in the action center (I think it disappears after 7 days, but not sure – I don’t keep my notifications for that long).

How it behaves

In the sample app, there are three buttons for launching toasts: one with no timeout, one with a 10 s timeout and one with a very short timeout (1 s).

Hitting them one after the other will display the toasts:

 

4 no timeout

5 10 s timeout

3 zero timeout

But looking in the Action Center will only show two, not the one with a very short timeout.

6 AC 1

And, waiting 10 s then invoking the Action Center again will show only the no-timout toast.

7 AC 2

Code

You’ll find the code in my sample app on github: https://github.com/andyhammar/ToastTestUniversal.

References

Managing toast notifications in action center (Windows Phone Store apps)
ToastNotification.ExpirationTime
Quickstart: Managing toast notifications in action center (XAML)
Quickstart: Sending a toast notification (XAML)
The toast template catalog (Windows Runtime apps)
ToastTemplateType enumeration

 

Happy toasting!

This Post Has 2 Comments

  1. Hi, how can i get the attributes when the push is received? In launch parameter inside the tag “toast”, i only get this attribute when the user click on the push notification. In my case, i need the attribute when the push arrived at the smartphone of the user.

    1. You can only run code directly when the push is received if your app is running (=in the foreground). If your app is suspended and a toast is displayed to the user – your code won’t run unless the user taps on the toast.

      To receive data when your app is running you should send a RAW notification, guidelines:
      https://msdn.microsoft.com/en-us/library/windows/apps/hh761463.aspx

Leave a Reply

Close Menu