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.

Your app should go through this short process to obtain the necessary channel URIs whenever it’s<br />

launched as well as when it’s resumed (especially if any channel’s expirationTime has passed). It’s<br />

unlikely that an app would stay suspended for that long, but it’s still possible! Furthermore, if you’re<br />

concerned that your app might not run for more than 30 days, you can implement a background task<br />

on a maintenance trigger for this purpose. See “Tasks for Maintenance Triggers” later in this chapter and<br />

Scenario 2 of the sample.<br />

Again note that you may have more than one channel URI if you’re also using push notifications for<br />

secondary tiles as well as your app tile. In this case you’ll be managing a separate channel URIs for each<br />

tile.<br />

Each time through the process, save the channel URI for each tile in your local app state. This is so<br />

that you can check on subsequent runs if the URI is the same as one you’ve already obtained and sent to<br />

your web service, in which case you can avoid unnecessary network traffic.<br />

Sending the URI to your web service can be done with a simple call to WinJS.xhr, as in the sample<br />

(inside channelOperation.done). Here we also see the checks for whether the URI is the same as before:<br />

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

// _urls[] is an array of channel ids for primary and secondary tiles<br />

var tileData = that._urls[itemId];<br />

// Upload the channel URI if the client hasn't recorded sending the same<br />

// uri to the server<br />

if (tileData && newChannel.uri === tileData.channelUri) {<br />

// This saves the URI to local app data<br />

that._updateUrl(url, newChannel.uri, itemId, isPrimaryTile);<br />

completed(newChannel);<br />

} else {<br />

WinJS.xhr({<br />

type: "POST",<br />

url: url,<br />

headers: { "Content-Type": "application/x-www-form-urlencoded" },<br />

data: "channelUri=" + encodeURIComponent(newChannel.uri) +<br />

"&itemId=" + encodeURIComponent(itemId)<br />

}).done(function (request) {<br />

// Only update the data on the client if uploading the channel URI succeeds.<br />

// If it fails, you may considered setting another background task, trying<br />

// again, etc. (An exception will be thrown if it fails, ending up in the<br />

// error hander instead.)<br />

that._updateUrl(url, newChannel.uri, itemId, isPrimaryTile);<br />

completed(newChannel);<br />

}, failed);<br />

}<br />

}, failed);<br />

609

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

Saved successfully!

Ooh no, something went wrong!