Windows Phone 7 Development : Push Notifications - Introducing the Push Notifications Architecture

5/5/2011 3:57:40 PM
Windows Phone push notifications involve three players, a phone application, a Microsoft service, and a remote web-based service, as illustrated in Figure 2. At the heart of push notifications is a service provided by Microsoft called, very appropriately, Microsoft Push Notification Service (MPNS). MPNS "provides a dedicated, resilient, and persistent channel for pushing a notification to a mobile device," according to MSDN documentation. This service is hosted on Microsoft's Azure cloud operating system and is responsible for seamlessly establishing a channel of communication between a Windows Phone 7 device and an application that provides notification data to it. Typically, notifications to Windows Phone 7 devices are provided by a web service (or a "cloud service," as often seen in Microsoft documentation). These cloud services are generally accessible via standard web protocols, such as REST and SOAP, aside from MPNS for data retrieval and updates.

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:

  1. Windows Phone 7 application contacts MPNS and requests for communication channel inside MPNS to be created.

  2. MPNS responds with the URI of communication channel, which is unique for a given Windows Phone 7 device and an application on that device.

  3. Windows Phone 7 application communicates the URI of the channel created in Step 2 to the service providing stock quotes.

  4. The stock quotes service publishes updates to the URI communicated to it in Step 3.

  5. 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.

  •  Windows Phone 7 Development : Push Notifications - Understanding Push Notifications
  •  Windows Phone 7 Development : Handling Multiple Concurrent Requests with Rx.NET
  •  WAP and Mobile HTML Security : Application Attacks on Mobile HTML Sites
  •  WAP and Mobile HTML Security : Authentication on WAP/Mobile HTML Sites & Encryption
  •  iPhone Application Development : Displaying and Navigating Data Using Table Views - Building a Simple Table View Application
  •  iPhone Application Development : Understanding Table Views and Navigation Controllers
  •  Windows Phone 7 Development : Revising WeatherRx to Manage Slow Data Connections
  •  Windows Phone 7 Development : Handling Data Connection Issues with Rx.NET
  •  Windows Phone 7 Development : Handling Errors in Rx.NET
  •  Windows Phone 7 Development : Using Rx.NET with Web Services to Asynchronously Retrieve Weather Data
  •  Windows Phone 7 Development : Media - Adding Sounds to an Application
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 4) - Implementing the Summary View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 3) - Implementing the Volume View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 2) - Implementing the Area View
  •  iPhone Application Development : Building a Multi-View Tab Bar Application (part 1)
  •  Windows Phone 7 Development : Working with Video (part 2) - Coding the Application
  •  Windows Phone 7 Development : Working with Video (part 1)
  •  Windows Phone 7 Development : Plotting an Address on a Bing Maps Map and Working with the Bing Maps Service
  •  Windows Phone 7 Development : Using GeoCoordinateWatcher and the Bing Maps Control to Track Your Movements
  •  iPhone Application Development : Creating a Multi-View Toolbar Application (part 3) - Adding Toolbar Controls
    Top 10
    Microsoft XNA Game Studio 3.0 : Displaying Images - Using Resources in a Game (part 4) - Filling the Screen
    Designing and Implementing Mobility in Exchange Server 2010: Securing Access to ActiveSync with Secure Sockets Layer Encryption
    ASP.NET State Management Techniques : The Role of the Global.asax File
    IIS 7.0 : Managing Configuration - Delegating Configuration (part 2)
    Building Your First Windows Phone 7 Application (part 1) - Creating a Windows Phone Project
    Web Security Testing : Manipulating Sessions - Analyzing Session Randomness with WebScarab
    iPhone Application Development : Creating a Navigation-Based Application
    Microsoft XNA Game Studio 3.0 : Working with Colors
    Mobile Application Security : Windows Mobile Security - Development and Security Testing (part 2)
    SQL Server 2008 : Using Temporary Tables in Stored Procedures
    Most View
    Windows Server 2008 : Configuring and Managing the Terminal Services - Load Balancing
    Protecting SQL Server Data : SCHEMA ARCHITECTURE STRATEGIES - Using Views
    - Mobile Application Security : Bluetooth Security - Bluetooth Security Features
    SQL Server : Exception Handling
    Exchange Server 2007 : Establish EdgeSync Subscriptions
    Creating Link-Worthy Content and Link Marketing : Types of Link Building (part 2)
    Windows Phone 7 : Submitting Your First Windows Phone Application to the Windows Phone Marketplace
    Programming .NET Security : Symmetric Encryption Explained (part 2) - Cipher Modes
    Windows Vista : Performing Local PC Administration (part 2) - Performing common workstation administration tasks
    Windows 7 :Navigating Your Computer with the Address Bar (part 1) - Accessing Locations on Your Computer
    Cloud Application Architectures : Database Management
    Business Intelligence in SharePoint 2010 with Business Connectivity Services : Consuming External Content Types (part 2) - Writing to External Content Types
    SQL Server 2008 : Advanced Stored Procedure Programming and Optimization - Using Extended Stored Procedures
    Saying Hello to Web Services
    Working with the Windows Phone 7 Application Life Cycle (part 2) - Managing Application State
    Building Android Apps: Going Offline - Debugging
    Advanced SharePoint 2010 Installation and Scalability : Scaling Logical SharePoint Components
    IIS 7.0 : Editing Configuration - Deciding Where to Place Configuration
    Configuring Mobile Connection Properties in Vista
    SQL Azure : Database Growth-Management Strategies