23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Managing Channel URIs (Service)<br />

If you use the code in the previous section, your web service that generates push notifications will<br />

receive HTTP POST requests with unique channel URIs for each and every tile. This isn’t the only way to<br />

transport channel URIs, of course; in fact, because a channel URI might be used to transmit personal<br />

information through notifications, it should ideally be encrypted with a private key before it’s sent to<br />

the server. Otherwise someone could possibly intercept that URI and use it to redirect user-specific<br />

notifications.<br />

In any case, the service must expect to receive—and then manage—a unique URI for each<br />

app/user/device combination. This underscores the fact that push notifications are best used for<br />

user-specific notifications rather than broadcast notifications. In the latter case, setting up a service for<br />

periodic updates is a much easier solution.<br />

Once the service receives a channel URI along with any data to identify the user and the purpose of<br />

the channel, it should securely save that information in persistent storage of some kind, such as a SQL<br />

Server database (for an ASP.NET service) or a MySQL (for a PHP service).<br />

It’s important that the service also removes obsolete channel URIs. If it receives a new URI for the<br />

same user and the same purpose, it should replace the old with the new. It should also remove any URIs<br />

from its data store if it receives an HTTP 404 or 410 error back from WNS, indicating an obsolete<br />

channel.<br />

A simple ASP.NET service page that can receive a post from Scenario 1 of the Push and periodic<br />

notifications client-side sample can be found in the HelloTiles website project in this chapter’s<br />

companion content, specifically receiveuri.aspx. To run this service, make sure you have the localhost<br />

established, as described earlier in the “Using the localhost” section for periodic updates. You may also<br />

need to install ASP.NET on your localhost. An easy way to do this is to obtain the Background transfer<br />

sample, go into its Server folder, and then from an administrator command prompt run powershell<br />

-ExecutionPolicy unrestricted -file serversetup.ps1. If you then run the site in Visual Studio Express<br />

2012 for Web as we did before, you should have a localhost port for the service (for instance,<br />

http://localhost:52568/HelloTiles/receiveuri.aspx).<br />

You can then set a breakpoint in the service code, paste the service URI into Scenario 1 of the Push<br />

notifications sample, and press its Reopen Channel And Send To Server button. This should hit the<br />

breakpoint in the service and allow you to step through the code that processes the request. Here you’ll<br />

find that the request contains channelUri and itemId values (along with LOGON_USER), which can be<br />

saved for when the service needs to send a notification to WNS. Something similar can be written in<br />

other server-side languages, of course, and a great place to find tools to help with writing services is the<br />

Windows Azure Toolkit.<br />

Sending Updates and Notifications (Service)<br />

Before a service can send updates, it must to authenticate itself with WNS by sending the Package<br />

Security Identifier (SID) and client secret as obtained through the Windows Store Dashboard. This is a<br />

610

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

Saved successfully!

Ooh no, something went wrong!