Home > Metro, Windows 8 > Windows 8 Toast Notifications

Windows 8 Toast Notifications

Every Windows 8 New UI Style app can display notifications, which will be shown on the right top corner like this:

image

User can ignore or close the messages using the Close symbol.

If the user clicks on the message, the the app will be invoked again and the code in app.xaml – OnLaunched() will be executed

 

Display toast notification

To display a notification the following code is needed:

  1. Fetch one of the predefined templates as XML:
    XmlDocument xmlDoc = 
        ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
    
    // ToastTemplateType.ToastText02 will return xml like this:
    //<toast>
    //  <visual>
    //    <binding template="ToastText02">
    //      <text id="1"></text>
    //      <text id="2"></text>
    //    </binding>
    //  </visual>
    //</toast>
    

  2. Update the toast data:
    XmlDocument xmlDoc = 
        ToastNotificationManager.GetTemplateContent
    (
    ToastTemplateType.ToastText02); // ToastTemplateType.ToastText02 will return xml like this: //<toast> // <visual> // <binding template="ToastText02"> // <text id="1"></text> // <text id="2"></text> // </binding> // </visual> //</toast>

  3. Get a ToastNotifier and show the notification:

    ToastNotifier toastNotifier = 
    ToastNotificationManager.CreateToastNotifier();
    ToastNotification toastNotification = new ToastNotification(xmlDoc); toastNotifier.Show(toastNotification);

 

Now the toast should be displayed. If not, then the user did probably disable toast notifications. This can be done either per app, globally or per user. See section below

Schedule toast notification

App can show a toast at a specific time. The app must not run at the given time.

This can be done using almost the same code as for displaying the notification. The only difference is, that a ScheduleToastNotification object must be created from the Xml.

This code will display the given notification at the specified time:

  1. Fetch one of the predefined templates as XML:
    XmlDocument xmlDoc = 
        ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
    
    // ToastTemplateType.ToastText02 will return xml like this:
    //<toast>
    //  <visual>
    //    <binding template="ToastText02">
    //      <text id="1"></text>
    //      <text id="2"></text>
    //    </binding>
    //  </visual>
    //</toast>
    

  2. Update the toast data:

    XmlDocument xmlDoc = 
        ToastNotificationManager.GetTemplateContent
    (
    ToastTemplateType.ToastText02); // ToastTemplateType.ToastText02 will return xml like this: //<toast> // <visual> // <binding template="ToastText02"> // <text id="1"></text> // <text id="2"></text> // </binding> // </visual> //</toast>

  3. Get a ToastNotifier and add the scheduled notification:
  4. ToastNotifier toastNotifier = 
    ToastNotificationManager.CreateToastNotifier();
    ScheduledToastNotification toastNotification =
    new ScheduledToastNotification (xmlDoc,

    new DateTimeOffset(DateTime.Now.AddSeconds(5)));

    toastNotifier.AddToSchedule(toastNotification);

 

Checking the notification setting


There are many possible ways to disable notifications. It can be done on user, app or global level. The current setting and the possible values can be fetched using this code:

var setting = ToastNotificationManager.CreateToastNotifier().Setting;
switch (setting)
{
    case NotificationSetting.DisabledByGroupPolicy:
        break;
    case NotificationSetting.DisabledByManifest:
        break;
    case NotificationSetting.DisabledForApplication:
        break;
    case NotificationSetting.DisabledForUser:
        break;
    case NotificationSetting.Enabled:
        break;
    default:
        break;
}

Categories: Metro, Windows 8
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: