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.

thousands or even millions of channel URIs. For this reason, seriously evaluate whether periodic<br />

notifications will be sufficient for your scenario, especially for updates that aren’t user specific, because<br />

they will be much simpler on the service side of the picture.<br />

Requesting and Caching a Channel URI (App)<br />

Requesting a channel URI is done through the<br />

Windows.Networking.PushNotifications.PushNotificationChannelManager object. This manager has<br />

only two methods: createPushNotificationChannelForApplicationAsync and<br />

createPushNotificationChannelForSecondaryTileAsync. The first is clearly linked to the app tile as well<br />

as toast notifications; the second is clearly for use with secondary tiles and takes a tileId argument to<br />

identify the specific one.<br />

The result of both async operations is a PushNotificationChannel object that will be passed to your<br />

completed handler, as shown in Scenario 1 of the sample (start in js/scenario1.js, then go into<br />

js/notifications.js):<br />

var channelOperation;<br />

// Channel for the app tile<br />

if (isPrimaryTile) {<br />

channelOperation = Windows.Networking.PushNotifications.PushNotificationChannelManager<br />

.createPushNotificationChannelForApplicationAsync();<br />

} else {<br />

// Channel for a secondary tile<br />

channelOperation = Windows.Networking.PushNotifications.PushNotificationChannelManager<br />

.createPushNotificationChannelForSecondaryTileAsync(itemId);<br />

}<br />

channelOperation.done(function (newChannel) {<br />

// Send channel to web service<br />

}, /* error handler */<br />

);<br />

The PushNotificationChannel object (newChannel in the code above) is a simple object with just a<br />

few members, but they are important ones:<br />

• expirationTime A read-only property indicating when the channel expires—notifications sent<br />

to this channel after expiration will be rejected. Apps must be sure to refresh their channels when<br />

needed to avoid an interruption in notifications.<br />

• uri A read-only URI to which the app’s web service sends notifications to WNS.<br />

• close A method that explicitly invalidates the channel.<br />

• pushnotificationreceived An event that’s fired when a notification is received on the client<br />

device from this notification channel. This will be fired only for apps that are in the foreground.<br />

608

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

Saved successfully!

Ooh no, something went wrong!