In the last post, we learned how to write an Azure worker role to poll stocks from the Yahoo! Finance service for a particular user and send them a push notification. It works, trust me; it’s just that we have no way of knowing because there is no data in your database. Only your phone / emulator knows the push notification URI that is unique to that combination of phone and app, so it’s the phone’s job to tell our system what exactly that is.
To make that possible, we’ve created a WCF Web Role which will expose an endpoint to the greater Internets™. This way, your phone will always have a way of registering with our service, as long as it has a data connection. We’ll be running and testing this on our local machines, but it’s just as easy to move it to the real world because all you need to do is change a configuration setting in the phone app.
This article focuses on the PushRegService WCF Web Role we created when we set up the Azure solution, so go ahead and open that project now.
Creating the Registration Service
- The first thing to do is rename the generated code. I guess somewhere along the line I decided I wanted the service to be called UserService since it also handles the creation of user accounts, not just registration for push notifications. So, rename the interface (IService1) to IUserService and rename Service1 to UserService. This should be reflected both in the filenames and the code just for the sake of completeness.
- Replace the code in IUserService.cs to the interface below. This lays out the data and operation contracts for the service. The data contract is explicitly identical to the table format of the Users table in our SQL Azure database. We have three methods here: RegisterForPush, UnregisterForPush and CreateNewUser. The only method we’ll really be testing, or caring about for the purposes of this exercise, is RegisterForPush.
- Make sure you add a project reference to the Utilities project as well as a reference to System.Data.Entity. In PushRegService.svc.cs add a using directive for Utilities.ORM. Also, delete what is already there in the class and re-implement the interface using IntelliSense. This will give you all the method stubs.
It’s possible that you’ll get an error later when you debug this service, along the lines of “this connection is not meant to be used by the EntityProvider” or something like that. That might mean you need to add the connection string from the Utilities project’s App.Config into the web.config for the WCF service. Alternatively you can abandon your hopes of reuse and just create another edmx locally if you just want to see this work.
Implementing the Registration Functionality
I’ve chosen to do this in as bare-bones a manner as possible, mainly so that we can just see this whole mess working together as quickly as possible. In the real world, you’d have an elaborate user account system, where you have an interface to create a new account, then verify the email, then log-in, etc. We don’t have the time or the space on this blog to do all that () so, instead, we’re just going to do it all-at-once. What that means is that the user will be presented (in the WP7 app) with a single screen that has fields for user, password, stocks to watch and whether or not to receive notifications. When the user hits Register, it will hit the service. If the account doesn’t exist, we’ll create one and set those preferences. If the account does exist, we log in with the provided password and update the existing fields.
The first thing we’ll do is implement the RegisterForPush method, since that’s all we are really going to test at this time. In code you can’t see, the UserService class is marked with a ServiceBehavior attribute that allows exceptions to pass through (IncludeExceptionDetailsInFaults = true). This makes debugging WCF services a lot easier especially when you are debugging them by way of a WP7 application.
Line 5 declares our Entity Framework instance that allows easy access to the database.
First we check to see if the user exists; if not, we create a new user. Then, we check the password, which is hashed with SHA1. If they don’t match, we throw an exception. If they do, we set that user’s preferences and save the changes. Congratulations! We’ve successfully registered a user for push notifications.
Here is the full code for the User Service:
What does “registering for notifications” really mean?
You’ll notice one of the arguments to this operation is the push notification URI. This URI is generated when the phone attempts to subscribe to a notification channel (more on that later). That just means that we get this value from code magic, and store it in the database for use later, kind of like you would with an OAuth access token. We don’t care what exactly it is, just that we have it; it’s the “key.” Once we have our push notification URI stored in a database and associated to a user’s record, we can send them push notifications whenever we like.
Application Certification and user options
One final note for this post is that you must inform the user that push notifications are being used in the app and give them an option to decline receiving them. If you want your app to pass certification, that is.
Up next, we’ll talk about building the Windows Phone mini-app itself, followed by a wrap-up post. We’re almost there!