Windows Phone 7 Development : Push Notifications - Introducing the Push Notifications Architecture
Naturally, you may be wondering what happens when MPNS becomes unavailable for some technical reason. At the moment, the answer is that should that happen, push notifications will not reach their destination, i.e., they will be simply dropped. Hence, push notifications should never be counted on as a reliable form of message delivery.
Figure 1. A tile notification
1. The Life Cycle of a Notification
Let's suppose that you're building a mobile stock trading application that consists of a central web service, which gathers and analyzes trading data, and an application running on Windows Phone 7 devices, which displays data to users. Suppose you want the application to notify users whenever there is a significant jump in the value of Microsoft stock, such as might occur right after the official launch of Windows Phone 7. Architecturally seaking, how would it go about doing that?
First, the application would need to take advantage of Push Notification Services, since this will allow it to keep the user updated on the market conditions even if the user is not constantly running the stock trading app on her Windows Phone 7.
Second, to enable push notifications on Windows Phone 7, a communication channel for notifications must be created between the application and the web service that is collecting stock trading data. In the world of push notifications, a communication channel is represented by a URI that contains all of the information necessary for notifications to reach their destination. A Windows Phone 7 client must request this channel be created, and obtain the URI associated with that communication channel. Third, the URI of this communication channel must be communicated to the web service that will deliver, or "push" notifications about the market conditions to Windows Phone 7 clients. Once this third step is complete, all three types of push notifications are available to the stock trading application.
Figure 2 provides detailed visual representation of how a Windows Phone 7 client application would be able to receive stock trading alerts from the server using MPNS. The steps are sequentially numbered and each numbered step is described in detail below:
Windows Phone 7 application contacts MPNS and requests for communication channel inside MPNS to be created.
MPNS responds with the URI of communication channel, which is unique for a given Windows Phone 7 device and an application on that device.
Windows Phone 7 application communicates the URI of the channel created in Step 2 to the service providing stock quotes.
The stock quotes service publishes updates to the URI communicated to it in Step 3.
MPNS routes the updates to the proper Windows Phone 7 device and application on that device.
When the service needs to send notifications to its clients, it contacts MPNS by making a POST request to the unique URIs created for each client (Step 4 in the outline above). These POST requests have to conform to a predefined XML schema set by Microsoft. The type of notification sent (toast, tile, or raw) is determined by one of the header values inside the POST request—namely, the "X-NotificationClass" header, and you will see how to use this header momentarily.
In the sections that follow, we'll show you how to write code for the steps that have just been described. Using the Windows Phone 7 emulator, you'll implement an application with both toast and tile notifications.
When programming Windows Phone 7 push notifications, perhaps the most common error that occurs is the PayloadFormat error. Generally, that means that the XML or message format received from the MPNS does not conform to the expected format. Things to check in that case would be whether an XML format expected for tile notifications is used for toast notifications or vice versa.
Figure 2. Basic push notifications architecture
2. The Push Notification Framework
The namespace whose APIs do the push notification heavy lifting is Microsoft.Phone.Notification, and the HttpChannelNotification is its workhorse. Before a notification of any type can be sent, a notification channel must be created. The HttpChannelNotification class allows developers to create a new channel or find an existing (previously created) one using its Open and Find
methods correspondingly. When programming push notifications, it is a
good practice to check whether the channel has been previously created
using the Find operation. You will see how this is done shortly in
several upcoming walkthroughs. An important note about the Openmethod:
once the channel is open, it is not immediately active. The push
notification channel becomes active once it acquires the push
notification URI from the MPNS.
Other important methods of the HttpChannelNotification class include BindToShellToast and BindToShellTile. These methods are responsible for associating, or binding, a particular HttpChannelNotification channel instance to toast and tile notifications. These methods have corresponding UnbindToShellToast and UnbindToShellTile methods that disassociate toast and tiles subscriptions correspondingly from a given channel. Finally, the Close method of the HttpChannelNotification class closes the channel and removes all the subscriptions associated with that channel.
Push notifications are most appropriate in situations where Windows Phone 7 applications almost fully depend on the data supplied by the server on the web or somewhere else in the cloud. As such, to demonstrate push notifications in action, you must create two separate projects: one project will be a Windows Phone 7 application, and the other project could be either a web-based application, a web service, or, for the purposes of keeping the current example simple, a Windows Forms application. To create a Windows Forms application that will be used as part of the next walkthrough, you will be using a version of Visual Studio that allows the creation of Windows Forms applications—Visual C# 2010 Express. Alternately, you could use other (paid) editions of Visual Studio 2010 to create Windows Forms applications.