Push Notifications for Windows Phone 7 Sample

This sample shows how to use push notifications within an XNA Framework application on Windows Phone 7.

Sample Overview

Push Notifications for Windows Phone 7 consists of two projects: a Windows Phone 7 client application and a Winforms sender application. When the client application is run, either in the emulator or on a device, it obtains and displays the unique URI for the application's notification channel. Copying this URI to the sender application allows test messages to be sent to the phone.

Sample Controls

This sample uses on-screen UI. The Back button also can be used to exit the sample.

How the Sample Works

The Push Notifications Sample uses a client (receiver) application and a sender application to illustrate sending and receiving three different types of push notifications: toast, tile, and raw.

The client application is a simple XNA Framework program. At startup, it attempts to open a push notification URI with HttpNotificationChannel.Find. If the channel exists it will be returned, and the HttpNotificationChannel.ChannelUri contains the unique notification URI. If the channel does not yet exist, the application creates a new channel with HttpNotificationChannel.Create. Additionally, the application creates handlers for the following events:

  • HttpNotificationChannel.ChannelUriUpdated. When a new push notification channel is created, or when there is a change to an existing channel, this event contains the new URI.
  • HttpNotificationChannel.HttpNotificationReceived. This event is triggered when a raw notification is received by the application.
  • HttpNotificationChannel.ShellToastNotificationReceived. This event is triggered when a toast notification is received by the application.
  • HttpNotificationChannel.ErrorOcurred. This event is triggered when there is a notification error such as a malformed payload. It may be necessary to close and reopen the notification channel in response to an error.

Once the client application's unique push notification URI is obtained from the notification channel, it is printed to the debugger output window and from there can be copied to the sender application. The sender application can test each type of push notification.

  • Toast Notifications. Toast consists of two strings of text. If the client application is running, the application handles the toast notification in the HttpNotificationChannel.ShellToastNotificationReceived event handler. If the channel exists but the application is not running or not in the foreground, toast appears in a pop-up window.
  • Tile Notifications. Tile (or Token) notifications cannot be handled by a running application and take effect only when the application is not running or not in the foreground, and when the user pins the application's shortcut to the Quick Launch menu. A tile notification can change the title and background image of the tile shortcut, as well as display a count value from 0 to 99.
  • Raw Notifications. Raw notifications can be handled only by a running foreground application, and both the client and server must agree on the format. In this sample, the raw notification transmitted is a simple XML document containing a string of data.

Extending the Sample

Developers can extend this sample by adding a simple web service to which the client application could send the notification channel URI, thereby eliminating the copy and paste step, and making the notifications operate more like a real world scenario.