Get started with push notifications in Mobile Services
This topic shows you how to use Windows Azure Mobile Services to send push notifications to a Windows Phone 8 app. In this tutorial you add push notifications using the Microsoft Push Notification Service (MPNS) to the quickstart project. When complete, your mobile service will send a push notification each time a record is inserted.
This tutorial walks you through these basic steps to enable push notifications:
- Add push notifications to the app
- Update scripts to send push notifications
- Insert data to receive notifications
This tutorial requires the following:
This tutorial is based on the Mobile Services quickstart. Before you start this tutorial, you must first complete Get started with Mobile Services.
Note
When you send less than 500 messages per user each day, you do not need to register or authenticate your mobile service app with MPNS.
Add push notifications to your app
-
Open the file App.xaml.cs and add the following using statement:
using Microsoft.Phone.Notification;
-
Add the following to App.xaml.cs:
public static HttpNotificationChannel CurrentChannel { get; private set; }
private void AcquirePushChannel()
{
CurrentChannel = HttpNotificationChannel.Find("MyPushChannel");
if (CurrentChannel == null)
{
CurrentChannel = new HttpNotificationChannel("MyPushChannel");
CurrentChannel.Open();
CurrentChannel.BindToShellTile();
}
} This code acquires and stores a channel for a push notification subscription and binds it to the app's default tile.
Note
In this this tutorial, the mobile service sends a flip Tile notification to the device. When you send a toast notification, you must instead call the BindToShellToast method on the channel. To support both toast and tile notifications, call both BindToShellTile and BindToShellToast
-
At the top of the Application_Launching event handler in App.xaml.cs, add the following call to the new AcquirePushChannel method:
AcquirePushChannel();
This guarantees that the CurrentChannel property is initialized each time the application is launched.
-
Open the project file MainPage.xaml.cs and add the following new attributed property to the TodoItem class:
[JsonProperty(PropertyName = "channel")]
public string Channel { get; set; } Note
When dynamic schema is enabled on your mobile service, a new 'channel' column is automatically added to the TodoItem table when a new item that contains this property is inserted.
-
Replace the ButtonSave_Click event handler method with the following code:
private void ButtonSave_Click(object sender, RoutedEventArgs e)
{
var todoItem = new TodoItem { Text = TodoInput.Text,
Channel = App.CurrentChannel.ChannelUri.ToString() };
InsertTodoItem(todoItem);
} This sets the client's current channel value on the item before it is sent to the mobile service.
-
In the Solution Explorer, expand Properties, open the WMAppManifest.xml file, click the Capabilities tab and make sure that the ID_CAP_PUSH_NOTIFICATION capability is checked.

This makes sure that your app can receive push notifications.
Update the registered insert script in the Management Portal
-
In the Management Portal, click the Data tab and then click the TodoItem table.

-
In todoitem, click the Script tab and select Insert.

This displays the function that is invoked when an insert occurs in the TodoItem table.
-
Replace the insert function with the following code, and then click Save:
function insert(item, user, request) {
request.execute({
success: function () {
// Write to the response and then send the notification in the background
request.respond();
push.mpns.sendFlipTile(item.channel, {
title: item.text
}, {
success: function (pushResponse) {
console.log("Sent push:", pushResponse);
}
});
}
});
} This registers a new insert script, which uses the mpns object to send a push notification (the inserted text) to the channel provided in the insert request.
Test push notifications in your app
-
In Visual Studio, select Deploy Solution on the Build menu.
-
In the emulator, swipe to the left to reveal the list of installed apps and find the new TodoList app.
-
Tap and hold on the app icon, and then select pin to start from the context menu.

This pins a tile named TodoList to the start menu.
-
Tap the tile named TodoList to launch the app.

-
In the app, enter the text "hello push" in the textbox, and then click Save.

This sends an insert request to the mobile service to store the added item.
-
Press the Start button to return to the start menu.

Notice that the application received the push notification and that the title of the tile is now hello push.
Next steps
In this simple example a user receives a push notification with the data that was just inserted. The channel used by MPNS is supplied to the mobile service by the client in the request. In the next tutorial, Push notifications to app users, you will create a separate Channel table in which to store channel URIs and send a push notification out to all stored channels when an insert occurs.