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.
First and foremost, don’t forget to make your app toast-capable in the app manifest! Easy to forget.
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:
<toast> <visual> <binding template="ToastText02"> <text id="1">headlineText</text> <text id="2">bodyText</text> </binding> </visual> </toast>
And to create a toast, the C# is:
var toastTemplate = ToastTemplateType.ToastImageAndText01; var toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate); var toastTextElements = toastXml.GetElementsByTagName("text"); toastTextElements.AppendChild(toastXml.CreateTextNode(text)); var toast = new ToastNotification(toastXml); ToastNotificationManager.CreateToastNotifier().Show(toast);
Toast in action center:
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:
toast.ExpirationTime = DateTime.Now.AddDays(1);
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:
But looking in the Action Center will only show two, not the one with a very short timeout.
And, waiting 10 s then invoking the Action Center again will show only the no-timout toast.
You’ll find the code in my sample app on github: https://github.com/andyhammar/ToastTestUniversal.
Managing toast notifications in action center (Windows Phone Store apps)
Quickstart: Managing toast notifications in action center (XAML)
Quickstart: Sending a toast notification (XAML)
The toast template catalog (Windows Runtime apps)