This was my recent session at the virtual tech days, a online event from Microsoft India : http://bit.ly/q4AFzZ 

Please see the demo source code and the PowerPoint decks from here : http://bit.ly/nf7Uld

The below was the agenda.

image

STEP1 : The story of Windows Azure & Devices

I set context as to what is Windows Azure in terms of  providing large pool of resources (tending to infinity) and completely accessible through REST based http call and this makes it completely accessible by variety of devices , tools and languages.

To gain context about windows azure , read on here : http://bit.ly/nY2z22 & to understand the synergy between azure & phone, a wonderful article in MSDN is here : http://bit.ly/mSbC8c 

STEP 2 : What is Microsoft Push notification Service

Next was to set context about Microsoft Push Notification Service. Check here to understand the basics of of this , the whys , what’s & how’s : http://bit.ly/o7o25H 

The scenario is very straightforward  -If we have a twitter app on a device (lets call it the phone), and we want to be notified of new tweets. We have the below options:

  • We need to keep app running and make the app poll the twitter services for newer updates
  • We can have a background process doing the same thing even when the app is not running

The above 2 options are very expensive on battery life of the device. The option is to have a 3rd party service pushing these notification to a centralized agent on the phone and the agent, then transmits the notification in various visual feels.

So i mean by this is there are 3 components here

  1. The Phone / device which has the twitter app(say) and a central Push notification agent running
  2. The cloud service , like the twitter service which has all the activities and data.
  3. The Microsoft Push notification Service which is a service provided by Microsoft which can relay/push the byte stream to the agent running on the device

So below are the process steps

image

Boxes in grey are components already available and boxes in orange are the code that developers need tow rite

  1. The twitter phone app tells the phone agent that it needs to subscribe for Push notification
  2. The phone agent requests the MPNS for a channelURI (unique) for them to get connected. As it stands today, one phone application can request only a single channelURI and a single phone can have a maximum of 15 ChannelURI only free
  3. The phone agent, relays the channelURI to the phone app(twitter app)
  4. The twitter app, then pushes the URI to the twitter service on the Cloud (or any WCF hosted service on the azure or any cloud)
  5. Once this set up is done, The cloud service, uses the channel URI to post any HTTP payload it needs to sent
  6. the MPNS relays the same payload through the Channel URI

Finally, these notifications can be of 3 types

  1. Tile Notifications:  Can upload the front tile of the device with background images, overlay counts and messages. This is particularly for Windows 8 & Windows Phones as of today. Tile graphic should be less than 80K and should download within 15 seconds
  2. Toast Notifications : A short message with subject and body. Gives a feeling of getting popped out a toaster and jumping on the screens like when u receive an SMS. When the application is not running the pop up toast effect comes, when the app is running, we , as developers can handle the reaction
  3. Raw Notification:  This posts a raw byte array of data less than 1k and can be received only when the app is running. This does not trigger any visible changes on the screen unless we can code for the same.

 

STEP 3 : File – New Demo

Setting up a demo needs the following components

  1. The WCF service which the application should subscribe to, is primeval. We will write an WCF service and host it as an azure Web Role (deploy to azure data centre)
  2. Create the Phone application that will receive all the notifications.
  3. Testing the push notifications

Before we start taking a look at the code and steps , let me get the list of all development tools that we need to have in place

  1. Microsoft Visual Studio 2010 Express: http://bit.ly/oLMdmV
  2. SQL Server 2008 R2 Express : http://bit.ly/rsOELR 
  3. Windows Azure SDK 1.5, September 2011 release : http://bit.ly/p3NtbE
  4. Windows Phone SDK and Silverlight toolkit for visual Studio : http://bit.ly/mUMiPO  & http://bit.ly/oG6nFf

WCF Service

We will create a WCF Service on the cloud. To have any device or phone application, we surely need a service that it can get notifications and data from. So let us create a cloud project and choose WCF web role as template and add the below as operation contracts

void SubscribeMyPhone(Guid phoneId, string ChannelURI);

void PushRawData(string rawMessage);

void PushToast(string toastTitle, string toastMessage);

void PushTileUpdate(string tileTitle, int tileCount, string tileImageURI);

We need implement these methods and please take a look at the PushNotSample in the attached sample.zip file here : http://bit.ly/nf7Uld

Next we need deploy this application to the windows azure data centre from Visual Studio 2010 . Please check on this article that gives all the details of the steps to publish your application to the azure data centre from Visual Studio 2010 : http://bit.ly/p3ey2x

Phone Application

Next is to create a client application, for the sake of the demo, we will use Windows Phone application.

  1. In Visual Studio, once you install the Windows Phone SDK and Silverlight for Windows Phone , you can see a new template on the file –>new >Project dialogue. Select “Silverlight for Windows Phone” and create an application. Add some textboxes for status message updates like you see in below snapshot
  2. Next is to add service reference and add the WCF cloud service handle (in my case, since the service is hosted on Windows Azure, this will be a xxxx.cloudapp.net URL)
  3. Now the main part is the use of “HttpNotificationChannel” which is in the “Microsoft.Phone.Notification”namespace  (this can help get a ChannelURI from the MPNS that is unique for the device and the phone app). Current 500 push messages per ChannelURI per day is given free from the MPNS service. We need to implement the following events for this object
    • ChannelURIUpdated : Once the Channel URI is updated from the MPNS service, we will need to bind the URI to the Tile & Toast shell templates of the Phone. This is done thru the “BindToShellToast” & “BindtoShellTile” APIs. Tile binding is slightly complex in the sense we need to allow the domain where the WCF service is hosted.
    • HttpNotificationRecieved : This is used to push a Byte Array of raw data to a phone app. That is, for RAW notification. We, as developers should handle how/what to display this. In the sample, we are display the raw data in a Text Area.
    • ErrorOccured : Update a Text Area with the error message, in case
  4. ServiceReference Client Object has a method for SubscribeMyPhone and we need to call this method once we have the ChannelURI and handle the “completed” for this. In the demo, on successful call back of this method, we are displaying in a Text Area – “Subscribed”.

Please see the below snapshots of the Phone emulate just when the application is launched and after the Subscribed & ChannelURI is got.

Finally look at the PushClient sample in the source code located in the sample.zip at http://bit.ly/nf7Uld

image                                                   

image

Testing the Application

The last part of the demo is really about running this application and seeing everything in action.

  • Run the PushClient phone app and see the application the Phone emulator. Pin the application to the start screen. You can see the snapshot on the left
  • You can use the WCFTestClient found in the Visual Studio Installs folder\Common7\IDE\WCFTestClient.exe and run the same. Here you can send the Raw, Toast & Tile notifications. In the snapshot below, we r sending a Tile notification.
  • Soon, u will that the background image, counter and the message appearing as a Tile Notification.

 

image   

image 

image

That’s it!!! You now have a basic windows phone application, that can receive notifications from a WCF service hosted on Windows Azure Web role through the Windows Push Notification Service.

Once again

Happy Coding !