Windows Phone 7 Push Notification For Beginners–The Device

OK… it’s been almost 2 months since I tried to start this series and now I’m going to finish it. A brief warning: this is done with pre-Mango bits so it won’t have all the latest, greatest most exciting things like the back-tile functionality or deep linking. (Here’s a good blog post on all that.) But it remains a good introduction.

Windows Phone 7 Push Notification Project Files (github)

The goal of this post is to provide a sample Push Notification application that is

  • easy to test
  • good architecture
  • provides a sample web app for testing your push code (part of the delay has been because I’ve been trying to be a perfectionist on that web app. I’ve decided I’m never going to get around to making it perfect so this is the best I can do with the time I have)

Too many of the sample apps I’ve seen shove the push notification code in the xaml code-behind, leaving it to us to separate out the bits and translate it into a real project. This project uses MVVMLight binding and an event-driven architecture to get the push stuff working.

So… let’s dig into it.

I created a class called PushNotificationService that held a number of helpful little variables and objects. The most important variable here is

private static string _pushChannelName = "SampleAppNotification";

This name will delineate our push service from other services. If something weird happens and your push service stops working, the first thing you should do is change this string.

Next, we have a method that allows us to control when we get the push notification Uri. As an overview of what we’re doing,

  1. See if we already have a valid HttpNotificationChannel (the object that will control the push permissions and handle information from the Microsoft Push server)
  2. If we do have a valid HttpNotificationChannel, make sure we got the channel Uri correctly (the uri we will send out push notifications to). If we did, SUCCESS! Otherwise, go to step 3.
  3. Set the event handlers so we can handle when we get a new channel Uri back from the Microsoft Push server.
  4. Ask the Microsoft Push server for a new channel Uri.
  5. Set the HttpNotificationChannel up to handle the right kind of push notifications.
  6. Handle the returned uri from the Microsoft Push server & send it to the rest of the app.

Step 1

_notificationChannel = HttpNotificationChannel.Find(_pushChannelName);

Step 2

if (_notificationChannel.ChannelUri != null)
      // Success! We run the event to send that info back to our ViewModel 

Step 3

    _notificationChannel = new HttpNotificationChannel(_pushChannelName);
    _notificationChannel.ChannelUriUpdated +=
         new EventHandler<NotificationChannelUriEventArgs>(_notificationChannel_ChannelUriUpdated);
    _notificationChannel.HttpNotificationReceived +=
         new EventHandler<HttpNotificationEventArgs>(_notificationChannel_HttpNotificationReceived);
    _notificationChannel.ErrorOccurred +=
         new EventHandler<NotificationChannelErrorEventArgs>(_notificationChannel_ErrorOccurred);

Step 4


Step 5


Binding Details

In the pre-Mango push notifications, there are 3 kinds of push notifications and we have to announce what kind of notifications we plan on handling.
First, there are toast notifications, which look like this (stolen from Shawn Wildermuth):

Toast notifications consist of

  • a title (“Toast Message” in the example above)
  • a message (“This is from the server”)

To bind them, we simply type:


Tile notifications will only show up if the user has pinned the application to the front application tile board on the phone. I like to split Tile notifications into two types because we have to handle them differently in the code.

The first kind of tile notification is what I call “simple” Tile notifications. With simple Tile notifications, we can change a number and title on our app tile. The new title will show up where the title always shows up (at the bottom left) and the number will show up in a black circle at the top right.


Doing this much is simple:


But then there are Live Tiles, which allow us to send a whole new image to the tile to change it in a more holistic way to represent some push data. (We can still update numbers and titles too.) Live Tiles, however, require that we whitelist all the websites that might want to send an image to our phone. So we need to create a collection of whitelisted domains and add that to our tile binding.

System.Collections.ObjectModel.Collection<Uri> permittedImageHosts = new System.Collections.ObjectModel.Collection<Uri>();
permittedImageHosts.Add(new Uri(""));

If you’re going to use my handy-dandy WP7 Push Tester, make sure you whitelist the exact domain above.

And that’s it… that’s all we need to do to get everything bound and ready to go! All we need now is to get the Uri from the Microsoft Push server so we know where to send our Push Notifications to.

Step 6

void _notificationChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)

This will run when we get the updated Uri back and we just send it in our event, which the ViewModel is listening to.

I’ve really streamlined the code in this blog post to deal with just the bare essentials. If you want to look at the project in detail, you can download it here.

Now, all we have to do is start testing it.

You can test it using my WP7 Push Notification web app. I’m also working on another post that walks through creating a push notification system using PHP, but in the meantime you might want to check out this PHP library that I used.

3 thoughts on “Windows Phone 7 Push Notification For Beginners–The Device

  1. If you are going for best contents like I do, only pay a quick visit
    this web site everyday as it offers feature contents, thanks

    Feel free to surf to my webpage :: Garcinia Cambogia Australia – Nereida

  2. Hi i want to know one thing which might be vry simple..

    IS it possible to send the push notifications from one windows mobile to another windows mobile

  3. Hello! I have some problems with my php service. I use php for send Tile update and Toast update requests to MS Notification Service. After first request in response I have following headers:


    RESPONSE: HTTP/1.1 200 OK Cache-Control: private Server: Microsoft-IIS/7.5 X-DeviceConnectionStatus: Connected X-NotificationStatus: Received X-SubscriptionStatus: Active X-MessageID: da8aa692-e9cd-682b-fe1c-22ffe7379414 ActivityId: cd9c8b0b-26db-4eac-835d-9f9505d7a381 X-Server: DB3MPNSM016 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Wed, 20 Jul 2011 11:21:42 GMT Content-Length: 0 Array ( [X-SubscriptionStatus] => Active [X-NotificationStatus] => Received [X-DeviceConnectionStatus] => Connected )

    But in my WP7 app I have nothing. When I try update tile again:


    RESPONSE: HTTP/1.1 404 Not Found Cache-Control: private Content-Type: text/html Server: Microsoft-IIS/7.5 X-DeviceConnectionStatus: Disconnected X-NotificationStatus: Dropped X-SubscriptionStatus: Expired X-MessageID: {B6AAC154-DA37-277F-14FE-9F76365ACDD1} ActivityId: f6b6a06b-a836-449c-9c9b-dc3033850981 X-Server: DB3MPNSM033 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Wed, 20 Jul 2011 11:29:34 GMT Content-Length: 1245

    It seems that after first request my channel was dropped.

    I do not have any problems in my WP7 app becouse when I use WindowsService all works fine.

    Please tell me if you have any ideas.

Comments are closed.