23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

In the running app, the pushnotificationreceived event is fired for the other notification types as<br />

well. Scenario 3 of the sample shows this in its event handler—I’ve modified this code a little bit for<br />

simplicity:<br />

function startListening() {<br />

// Assume channel has been obtained and validated<br />

channel.addEventListener("pushnotificationreceived", pushNotificationReceivedHandler);<br />

}<br />

function pushNotificationReceivedHandler(e) {<br />

// Extract notification payload for each notification type<br />

var notificationPayload;<br />

switch (e.notificationType) {<br />

case pushNotifications.PushNotificationType.toast:<br />

notificationPayload = e.toastNotification.content.getXml();<br />

break;<br />

case pushNotifications.PushNotificationType.tile:<br />

notificationPayload = e.tileNotification.content.getXml();<br />

break;<br />

case pushNotifications.PushNotificationType.badge:<br />

notificationPayload = e.badgeNotification.content.getXml();<br />

break;<br />

}<br />

case pushNotifications.PushNotificationType.raw:<br />

notificationPayload = e.rawNotification.content;<br />

break;<br />

}<br />

// Process the notification: set e.cancel to true to suppress automatic handling.<br />

The last bit in the comment above is important. When you receive this event in a running app, it<br />

wouldn’t be necessary to display a toast unless it pertains to some other part of the app that isn’t visible.<br />

For example, if you have an app that handles both email and a calendar, you might want to show email<br />

toasts when the user is looking at the calendar and calendar toasts when the user is looking at email. In<br />

this case, setting e.cancel to true will suppress the toast.<br />

With the pushnotificationreceived event, the running apps gets first crack at raw notifications. If<br />

the app doesn’t process it, the notification will be sent to any lock screen background task configured<br />

for the PushNotificationTrigger. In either case, refer to the Raw notifications sample for more details.<br />

613

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!