Toast Notifications

In this example we will show you how to add a scheduled toast notifcation. Scheduled notifcations are useful to remind the user of an upcoming event - in this can one that they RSVP'ed to in the app.


Add a Toast Notification to your app

1

The first thing we have to do is open up RSVP.xaml.cs from the solution explorer.

Find the following event handler:

private async void sendButtonHandler(object sender, RoutedEventArgs e)
2

We now want to add a toast notification that will pop up when the event is scheduled by the user.

This will remind the user of the event.

if (yesRadioButton.IsChecked == true)
{
...
}
3

Next we want to configure the notification text which is shown in the toast. This is specified as XML within a string:

// setup notification text
string TOAST = $@"
<toast>
    <visual>
        <binding template=""ToastGeneric"">
            <text>Event Alarm</text>
            <text>Check your calendar for " + App.EventModel.EventName + $@"</text>
        </binding>
    </visual>
    <actions>
        <action content = ""Done"" arguments=""cancel""/>
    </actions>
    <audio src =""ms-winsoundevent:Notification.Reminder""/>
</toast>";
4

Now we want to set when the toast should appear. For demo purposes you can set this to 10 seconds. To make it work for real uncomment out the EventStartTime line:

// set when the notification should be shown or demo purposes in 10s
var when = DateTime.Now.AddSeconds(10);

//var when = App.EventModel.EventStartTime;
var offset = new DateTimeOffset(when);
5

Finally we use ScheduledToastNotification to create the toast itself

Windows.Data.Xml.Dom.XmlDocument xml = new Windows.Data.Xml.Dom.XmlDocument();

xml.LoadXml(TOAST);

// create the notification
ScheduledToastNotification toast = new ScheduledToastNotification(xml, offset);
Random rnd = new Random();
toast.Id = rnd.Next(1, 100000000).ToString();

ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast);