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.

Similarly, change stopBadgePolling in js/scenario5.js to read as follows:<br />

function stopBadgePolling() {<br />

var updater = notifications.BadgeUpdateManager.createBadgeUpdaterForApplication();<br />

updater.clear();<br />

updater.stopPeriodicUpdate();<br />

WinJS.log && WinJS.log("Stopped polling.", "sample", "status");<br />

}<br />

Without these changes, old updates will persist on the tile even after you stop the updates. If you<br />

then change your web service but it has an error in the XML, you won’t see any change on the tile and<br />

might think that the update worked when it really didn’t. Trust me: making these small changes will<br />

simplify your life!<br />

Web Services for Updates<br />

Creating a web service for periodic updates means creating a web page at some given URI whose sole<br />

purpose is to respond to an XmlHttpRequest with XML content for a TileNotification or<br />

BadgeNotification object. Ideally, such a page also handles the scaling, accessibility, and localization<br />

parameters provided in the query string described earlier in “Using Local and Web Images.”<br />

The page can be implemented using whatever language and tools you want, such as PHP or<br />

ASP.NET. In fact, unless you really enjoy programming in Notepad, you’ll certainly want to utilize a good<br />

web development tool! Visual Studio Express for Windows 8 is not actually equipped for this task; the<br />

full version of Visual Studio 2012 is. You might also look into Visual Studio Express 2012 for Web as<br />

another option; more on this in a moment. If you use ASP.NET, remember again that you can again<br />

employ the Notifications Extensions Library for easily creating the tile XML.<br />

Some examples of pages that provide tile updates are given in the Creating a great tile experience<br />

(Part 2) post on the Windows Developers Blog. Based on those examples, here is a trivial (but functional)<br />

one-liner PHP page that will post XML for a badge update with the current day of the month:<br />

<br />

Drop this code into a .php file (see HelloTiles/dayofmonthservice.php in the companion content for<br />

this chapter) on whatever web server you might have access to and voila! There’s a very basic service<br />

that delivers badge updates. You can use this in Scenario 5 of the Push and periodic notifications<br />

client-side sample—enter your page’s URI in the box, press the button to start polling, and then check<br />

the sample’s tile on the Start screen tile. In a few seconds you should see the day of the month appear as<br />

a badge. (Of course, with this ultrasimplistic example the date will reflect the local time on the web<br />

server rather than the device, which could be completely mismatched. A real service would be sensitive<br />

to time zone and other locale-specific considerations.)<br />

592

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

Saved successfully!

Ooh no, something went wrong!