Push notifications programming overview (Android versus Windows Store apps)

Applies to Windows and Windows Phone

Learn about key similarities and differences between the push notifications programming models for Android and Windows Store apps.

Introduction

Push notifications are messages that are proactively sent by servers to registered apps. These messages typically result in an app changing its state. Examples of these app state changes are breaking news, social network updates, new email messages, and weather alerts. Both Android and Windows 8 support push notifications through specific cloud services.

Top

Android: Google Cloud Messaging (GCM)

The Google Cloud Messaging (GCM) process flow is shown in the following figure. GCM requires a Google account. An app uses this account to get a project number and an API key. The app registers for push notifications by sending a com.google.android.c2dm.intent.REGISTER intent along with the project number (see #1 in the figure). GCM broadcasts a com.google.android.c2dm.intent.REGISTRATION intent, including a registration ID, to the subscribed app. Typically the app forwards this registration ID to the server, which the server records in its database (see #2). The server creates the push notification by contacting GCM and providing the registration ID, the API key, and a message (see #3). The GCM service then stores and forwards the push notification message to the app (see #4).

Google Cloud Messaging process flow

Top

Windows 8: Windows Push Notification Service (WNS)

Windows Push Notification Services (WNS) is the equivalent of GCM for Windows 8 and uses a similar process flow for device registration and sending push notifications, as shown in the following figure. The GCM registration ID equivalent in WNS is the channel Uniform Resource Identifier (URI) (see #1 through #3 in the figure). It is used by the server (in this case Microsoft Azure Mobile Services (WAMS)) along with an API key when contacting WNS with the push notification message.

Windows Push Notification Service process flow

WAMS can be replaced with any custom web services infrastructure of the developer's choice. The following pseudocode snippet example shows the work involved in implementing the registration on the client sending push notifications to WAMS on the server.


// Client code: channel registration pseudocode.
public class Channel 
{
    public int? Id { get;set; } 
    public string Uri { get;set; }
}

sealed partial class App : Application 
{
    protected override async void OnLaunched(LaunchActivatedEventArgs args)
    {
        // ...
        // Get the push notification channel URI by using the info from the app manifest.
        var channel = await PushNotificationChannelManager;

        CreatePushNotificationChannelForApplicationAsync();
        Channel channelRow = new Channel() { Uri = channel.Uri; }
        await MobileService.GetTable<Channel>.InsertAsync(channelRow);

        // Save the channelRow.Id value that's returned by Microsoft Azure Mobile Services for future use.
        // ... 
    }
} 


In the preceding pseudocode example, when the app starts, it gets a channel URI and sends it to the server to be saved into a Channel table. This requires a schema-less Channel table to first be created in WAMS. When the InsertAsync method is called with the Channel object, WAMS automatically creates the table's column definitions and inserts the row into the Channel table. WAMS then uses this channel URI and the API key to send notifications through WNS.

Here's a corresponding server-based pseudocode snippet example.


// WAMS server code: JavaScript code on the server.
// Note that code for service authentication with Windows accounts and channel 
//   retrieval from storage is removed for brevity. 
function sendNotifications(message, channel) 
{ 
    push.wns.sendToastText04(
        channel.Uri, 
        { text1: item.text, text2: "value1", text3: "value2" }, 
        { 
            success: function(response) {
                console.log(response); 
            }, 
            error: function(err) { 
                console.error(err); 
            }
        }
    ); 
} 


WAMS implements a JavaScript programming model on the server and provides default handlers for INSERT, UPDATE, DELETE, and READ requests in JavaScript. Developers can replace the default handlers as needed with custom JavaScript code, for example in the following code snippet example that sends push notifications.


// WAMS server code: replacing the default insert handler for push notifications.
function insert(item, user, request) {
    request.execute(
        { 
            success: function() {
                // Respond, and then send the push notification in the background.
                request.respond();
                push.wns.sendToastText04(
                    item.channel, 
                    { text1: item.text }, 
                    {
                        success: function(pushResponse) 
                        { console.log("Sent push:", pushResponse); }
                    }
                );
            }
        }
    );
}

WAMS supports not only Windows 8 and Windows Phone 8 but other major platforms such as Android and iOS.

Top

 

 

Show:
© 2014 Microsoft