Windows Phone: Using Azure Mobile Services for push notifications and scheduled jobs

I’ve been working on a flight-tracking app for Windows/Windows Phone for a bit now, and one of the features I really want to add is push notifications for flight status updates. I’ve been using Azure Mobile Services which has been mostly great, but I had a little trouble putting the pieces together.

In my scenario the app is able to pin tiles for particular flights to follow, and I want to be able to push a notification to update the tile for a flight when its status changes.

1. Create a new Azure Mobile Service and integrate it into your app (or start a new one) using the guide there.

2. Follow this guide for integrating push notifications into your app.

Note that the code includes registering for a push notification channel for the whole app, which is great if you want to send toasts or update the app’s main tile, but if you want to update a secondary tile you’ll need to register a separate channel for that.

Now we get to the more app-specific stuff!

3. After the user pins a tile, you need to call into your mobile service and register that this client has an interest in this flight. I used a custom API to do this. This isn’t strictly necessary, but if the user later unpins that tile and no one else is tracking that flight I can stop tracking it in the mobile service.

Note that to run this code after the tile is pinned, you need to set up a Suspended handler.

4. When the user pins a tile, run the following code after it pins:

var channel = await PushNotificationChannelManager
.CreatePushNotificationChannelForSecondaryTileAsync(PinnedId);
await App.MobileService.GetPush().SecondaryTiles[PinnedId].
RegisterNativeAsync(channel.Uri, new string[] { PinnedId });

Here the PinnedId is a unique identifier for the flight. The last argument to RegisterNativeAsync() is an array of tags that the tile will respond to push notifications for.

Note that to run this code after the tile is pinned, you need to set up a Suspended handler.

5. At this point, you can pin a flight in your app, then can go to the Notification Hub in the Azure dashboard and send a sample push notification (under the Debug tab) to make sure that the tile is updating. Note that for a Universal Windows Phone app you need to select the “Windows” platform, and enter the PinnedId as the tag to send to.

6. Now it’s time to write a scheduled job! The jobneeds to update all the flights that have been registered for, and send push notifications appropriately. The trick is to send the notification with the same tag (i.e. the PinnedId) that the client registered for – that way, only clients that are interested in that flight will get updates. To do this, pass the PinnedId to SendAsync() like so:
await Services.Push.SendAsync(message, scheduledId);

Note that support for .NET scheduled tasks is pretty new to Azure Mobile Services, so I didn’t find much documentation about this. Here are two things I had some trouble with:
– To make a scheduled job, make a new class that extends from ScheduledJob – but the class name must end in “Job” (like “PushUpdaterJob”), and the job’s name on the Azure dashboard has to be the class name without the “Job” (i.e. “PushUpdater”)
– My scheduled job class didn’t seem to get deployed until I actually restart the whole mobile service. (there’s a “Restart” button at the bottom of the Dashboard page for the mobile service) I spent hours trying to figure out what was going on! It’s possible this was a temporary glitch, but it’s a good thing to try if you’re having problems.

That’s basically it! You can also add a Custom API call to unregister for flight updates if a tile gets unpinned if you’d like, and you can change the scheduled job to send a toast notification as well when flights change status.

Any questions about this? Feel free to drop me a line at greg@gregstoll.com!

See all my Windows Phone development posts. I also send out a monthly-or-so email with news for developers – check out the latest email and sign up here!

I’m planning on writing more posts about Windows Phone development – what would you like to hear about? Reply here, on twitter at @gregstoll, or by email at greg@gregstoll.com.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s