Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Display a Toast Message and Remove It When the Application Closes

0.00/5 (No votes)
22 Sep 2016 1  
A quick way to add a toast notification to uwp applications and remove when the app closes...

Introduction

This is my first tip on adding a toast notification to a uwp app.

I'm not a professional programmer and don't intend to be one. Just as a hobby, I program small applications.

I have also created a very simple and very basic demonstration application. You can download the sourcecode.

Background

For work and hobby, I change USB Comm ports a lot. You always hear a sound but never see a message like 'Serial USB device COM5 inserted'. I have created only for my personal use a simple and basic program in C# for UWP. This program listens for USB insertions en checks if it is a serial port. Then when I insert a USB Comm port device I see a toast message with the text "device COM5 inserted". For my very handy.

Using the Code

To display a toast, you need to set up a simple template.

//Display a toast message
private void displayToastNotification(String caption, String message)
{
  var toastTemplate = "<toast launch=\"app-defined-string\">" +
                      "<visual>" +
                        "<binding template =\"ToastGeneric\">" +
                          "<text>" + caption + "</text>" +
                          "<text>" + message + "</text>" +
                        "</binding>" +
                      "</visual>" +
                      "</toast>";

  var xmlDocument = new XmlDocument();
 
  xmlDocument.LoadXml(toastTemplate);
  
  var toastNotification = new ToastNotification(xmlDocument);
 
  var notification = ToastNotificationManager.CreateToastNotifier();
            
  notification.Show(toastNotification);
}
Colourised in 118ms

When the user clicks the button 'Toast' a toast message appears.

//Button Toast
private void btnToasTest_Click(object sender, RoutedEventArgs e)
{
  displayToastNotification("Hello world!", "This is my message!");
}
Colourised in 6ms

When the user clicks the button 'Close' all toast messages are remove from notification center

 //Button Close
private void btnCloseApp_Click(object sender, RoutedEventArgs e)
{
  removeToastHistory();
  Application.Current.Exit();
}
Colourised in 6ms

When the user closes the application, I want to clear the notifications too. I accomplished this with this simple function.

//Remove toast history
private void removeToastHistory()
{
  var toastHistory = ToastNotificationManager.History;
  toastHistory.Clear();
}
Colourised in 6ms

That's all....

Points of Interest

On the internet, I found a lot of examples how to create a toast notification but not how to neatly remove it when the application closes. I solved it like this.

History

  • No history yet..... I might post my simple USB COMM port watcher application in the future. For now only the very simple and very basic application for the toast only.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here