Thursday, 26 December 2013

Registering for Live Tile Pushes from Windows Azure Mobile Services

This last weekend I decided to get started on my Developer Movement apps.  I decided to make sure the new app had Windows Azure integration and what better way to do that by using Mobile Services. 

I was surprised to find out that there was not as much sample code available as I would have liked.  I created a very simple app that keeps a simple counter.  I wanted that counter to be pushed to a live tile.  Sounds simple right?  Well mostly it is, but, of course, I wanted to have a bit of flexibility so I decided to do it with JScript using the scheduler.  I handled the input of the data from the app using web services deployed to a web site but that is really for another conversation.   What I want to cover here is the push stuff.  What happens after the app is closed and Azure takes over.

Windows Azure mobile services are simply marvelous.  They not only give you a nice tidy way to read and write to the SQL Azure database but it has the built in ability to schedule pushes for your applications.  From the Mobile Services homepage for your application (first 5 are free right now) you have the ability to create databases, add an API that allows your mobile app access to that database, define and create PUSHes to your mobile app (toast and live tiles) and lastly you can schedule automatic pushes of those notifications.  And it works with Windows Phone, and most other platforms.  So write your backend once and then roll it out to multiple mobile platforms.  However, we are talking Live Tiles here and that means Windows Phone.

Right from the Windows Azure Mobile Services home page for your app it gives you great instructions on how to create a Live Tile push notifications including sample code (JScript for the Azure size and C# for the Windows Phone side).  The instructions are very clear and I was able to implement the Live Tile quite easily.

http://www.windowsazure.com/en-us/develop/mobile/tutorials/get-started-with-push-wp8/

There is one little tiny bit of information that is missing here though.  You see, the instructions include code for registering a particular app instance on a phone so that the push knows where to send the data.  However, to be polite, you really should always give the user the option to turn Live Tiles off.  At least I think its polite.  To do that you have to Unregister your app when requested.  By default you don't need to do anything on the Azure side.  If you followed the instructions in the link above, all is done for you.  What you do need to do is call a bit of code in your app when the user requests that pushes be turned off.  The important line there is the DeleteAsync but you need to make sure you have the correct channel identification.   This will work with the sample code linked above.

// In the AquirePushChannel method, save the ID when we get the registration
//   And save it someplace like isolatedstorage or just App.Xaml.cs.  
//   You could write something to return the ID, but this will just work.
//   immediately after the await  InsertAsync()
PushRegistration = registration.Id;

// See if we have a Channel already
CurrentChannel = HttpNotificationChannel.Find("MyPushChannel");

if (CurrentChannel != null)
{
    // We have a channel.  need to retrieve the ID from the 
    //   Registrations table
    IMobileServiceTable<Registrations> registrationsTable = App.MobileService.GetTable<Registrations>();
    var registration = new Registrations
    {
        // Get the ID we stored away when we started the App.
        Id= PushRegistration
    };
    //  Tell Windows Azure Mobile Services to delete our channel
    await registrationsTable.DeleteAsync(registration);
}

That's all there is to it.  Follow the rest of the instructions and you should have a nicely full functioning Live Tile App.

Enjoy!