Microsoft has release a WP7 recipe called the “Windows Phone Server Side Push Notification Helper”. The recipe consist of a demo application which shows how one can send toast, tile and raw notifications. I’m working on an app which requires toast notifications so I decided to try the server side implementation of the app because it really makes the task of pushing notifications from your server to the phone easy. Here’s the only code you need (after compiling and adding the WindowsPhone.Recipes.Push.Messasges into your project):

var message = new ToastPushNotificationMessage (MessageSendPriority.High) {Title = title, SubTitle = subTitle};
message.SendAsync(uri, OnMessageSent );

Nice and simple. But still I manager to spend almost five hours to get this working. My app was receiving the notifications but only on random basis. Sometimes I got the message, sometimes I didn’t. The only way I managed to receive the message with a 100% success rate was by starting the app, sending the message from the server and then closing the app. After the app closed, I got the notification. But what happened when I tried to send an another message after that? Nothing. The server reported that the device had received the message but it didn’t.

I was completely sure that my client was causing the problem. I think I rewrote the whole code about three times and at one point I was sure that my problem had something to do with the Caliburn.Micro. But no. Creating a new Panorama App from a scratch didn’t help at all and it was acting just like the other app.

And then, finally, I gave up and started looking at the server code. I compared the differences between the recipe’s demo application (which was working nicely all the time) with my implementation and then it hit me: The demo app was passing the MessageSendPriority.High in to the message’s constructor, I wasn’t. And what do you know, after making the change into my server the notifications started working as they should! No more randomness, the notifications are hitting my phone with a 100% success rate.

So what is the MessageSendPriority? It seems to be a wrapper around the "X-NotificationClass” –header. The MSDN says this about the possible values:

// Possible batching interval values:
// 2: The message is delivered ... immediately.
// 12: The message is delivered ... within 450 seconds.
// 22: The message is delivered ... within 900 seconds.

This is how the recipe is calculating the value:

int batchingInterval = ((int)SendPriority * 10) + NotificationClassId;

In my case the NotificanClassId was 2 (meaning ToastPushNotificationMessage). By default the SendPriority was Normal, which represents the integer value 1. So, without setting the MessageSendPriority, the messages are delivered with the batching interval value 12, meaning that the message should be delivered in 450 seconds. 

I’m quite sure that this isn’t working correctly because I’m still missing like 100 messages. And this isn’t only an emulator problem because my actual device is missing the notifications also. So the bottom line is, use the MessageSendPriority.High, or if you’re sending the notifications manually, use the following values in the X-NotificationClass -header:

  • Tile notification: 1
  • Toast notification: 2
  • Raw notification: 3