Mobile Zone is brought to you in partnership with:

Den is a DZone Zone Leader and has posted 460 posts at DZone. You can read more from them at their website. View Full User Profile

Sending tile push notifications on Windows Phone 7

08.24.2010
| 15898 views |
  • submit to reddit

For Windows Phone 7 applications, it is possible to bind it to a web service that will send push notifications. There are two types of notifications that can be sent to the device –toast and tile. Toast notifications pop up on top of the title bar, no matter what application is currently running to notify the user about a change related to the application or to the data that is processed by that specific application. Here is how you can send toast notifications.

On the other side, tile notifications only appear if the application was pinned to the Start screen. A tile notification can display a number in the topmost right corner of the application tile – also known as the badge. The background image for the tile can be changed and so can the title. All this is handled via HTTP requests.

Basically, if you take a look at the article I’ve mentioned above, pretty much the same code base is used with small modifications in the headers that are used for the request and the XML contents that define how the notification will appear on the phone.

So let’s take a look at the channel setup code for the notification:

HttpNotificationChannel channel = HttpNotificationChannel.Find("NotificationTest_WP7");

if (channel == null)
{
channel = new HttpNotificationChannel("NotificationTest_WP7");
channel.Open();
}

if (!channel.IsShellTileBound)
channel.BindToShellTile();

I used this code in the App constructor that is called when the application is launched. If there is no channel, a new one is created. In case there is one, I am checking whether it is already bound to shell (notice that I am using BindToShellTile instead of BindToShellToast, since it is a tile notification).

NOTE: Sometimes you might get an error, saying that the channel is not open. In that case, simply restart the debugging session. There seems to be an issue with the Live service from time to time that will not let you connect.

The service that is going to handle the requests (you can see how I implemented a sample server in the article I referenced above) is structured like this:

string channelURI = "YOUR_CHANNEL_URI_HERE”;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(channelURI);
request.Method = "POST";
request.ContentType = "text/xml";
request.Headers.Add("X-NotificationClass", "1");
request.Headers.Add("X-WindowsPhone-Target", "token");

string notificationData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
"<wp:Tile>" +
"<wp:BackgroundImage></wp:BackgroundImage>" +
"<wp:Count>99</wp:Count>" +
"<wp:Title>TEST</wp:Title>" +
"</wp:Tile> " +
"</wp:Notification>";

byte[] contents = Encoding.Default.GetBytes(notificationData);

request.ContentLength = contents.Length;

using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(contents, 0, contents.Length);
}

string notificationStatus;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
notificationStatus = response.Headers["X-NotificationStatus"];
}

The HTTP headers sent to the service are a bit different. For example, I am using token instead of toast as X-WindowsPhone-Target. This will specify that a tile notification will be used.

If you take a look at the structure of XML contents that are sent, you can see the BackgroundImage tag, that defines the background of the tile. According to the current specifications, if the image is stored in a remote location, its size should not be more than 80KB and it shouldn’t take more than 1 minute to download.

What I’ve noticed here, is that if this specific requirement fails, the tile won’t be updated at all, although the status of the notification will be set as Received.

The Count tag defines what will be the number displayed as the badge. An important thing to understand is that this number does not represent any specific notification numbers, but is obtained from the server. Therefore, if there are two updates pending, do not expect the number to automatically change if you send two separate updates. The number should be directly obtained from the server.

The maximum number that can be passed is 99. If you pass a larger value, 99 will be the one displayed anyway.

And the Title tag defines what will be the title for the tile when the notification is received.

At early debugging stages, I highly recommend taking a look at the HTTP response, since it carries some important information regarding the state of the connection to the channel, as well as some information regarding the message that was sent.

Now when everything is ready to receive the notification, exit the application in case you were debugging it (it still should be deployed to the emulator). Pin it to the Start page (here is how to do it) and launch the service. Once the notification is sent, you can see a result similar to this:

I didn’t pass the image for the request, so the default tile image is used. In case you want to modify the default tile image, you can go to Properties (in the Solution Explorer) and change the Tile Options: