Export (0) Print
Expand All
6 out of 12 rated this helpful - Rate this topic

How to: Send a Push Notification for Windows Phone

1/28/2011

After you have set up your web service, and understand the Types of Push Notifications for Windows Phone, you can use the following code examples to send push notifications to your application.

The following code example describes how to send a push notification from a web service to the Microsoft Push Notification Service.

// The URI that the Push Notification Service returns to the Push Client when creating a notification channel.
string subscriptionUri = "<Notification Channel URI>";
HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);

// HTTP POST is the only allowed method to send the notification.
sendNotificationRequest.Method = "POST";

// The optional custom header X-MessageID uniquely identifies a notification message. If it is present, the // same value is returned in the notification response. It must be a string that contains a UUID.
sendNotificationRequest.Headers.Add("X-MessageID", "<UUID>");

// Sets the notification payload to send.
byte[] notificationMessage = new byte[] {<payload>};

// Sets the web request content length.
sendNotificationRequest.ContentLength = notificationMessage.Length;

using (Stream requestStream = sendNotificationRequest.GetRequestStream())
{
   requestStream.Write(notificationMessage, 0, notificationMessage.Length);
}

// Sends the notification and gets the response.
HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
string notificationStatus = response.Headers["X-NotificationStatus"];
string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];

Replace the byte array and <payload>. For example:

new byte[] {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};

Additionally, add the following HTTP header:

sendNotificationRequest.Headers.Add("X-NotificationClass", "<batching interval>"); 

// Possible batching interval values:
// 3: The message is delivered by the Push Notification Service immediately.
// 13: The message is delivered by the Push Notification Service within 450 seconds.
// 23: The message is delivered by the Push Notification Service within 900 seconds.

For more information about how to create or modify an application Tile, see How to: Create and Modify an Application Tile for Windows Phone.

Add the following HTTP headers:

sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "token");
sendNotificationRequest.Headers.Add("X-NotificationClass", "<batching interval>"); 

// Possible batching interval values:
// 1: The message is delivered by the Push Notification Service immediately.
// 11: The message is delivered by the Push Notification Service within 450 seconds.
// 21: The message is delivered by the Push Notification Service within 900 seconds.

Replace <payload> in the preceding code example with the following:


string tileMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
   "<wp:Tile>" +
      "<wp:BackgroundImage><background image path></wp:BackgroundImage>" +
      "<wp:Count><count></wp:Count>" +
      "<wp:Title><title></wp:Title>" +
   "</wp:Tile> " +
"</wp:Notification>";

<background image path>, <count> and <title> are in a string format.

If <background image path> references a remote resource, the maximum allowed size of the Tile image is 80 KB, with a maximum download time of 15 seconds.

Add the following HTTP headers:

sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
sendNotificationRequest.Headers.Add("X-NotificationClass", "<batching interval>");

// Possible batching interval values:
// 2: The message is delivered by the Push Notification Service immediately.
// 12: The message is delivered by the Push Notification Service within 450 seconds.
// 22: The message is delivered by the Push Notification Service within 900 seconds.

Replace <payload> in the preceding code example with the following:


string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
   "<wp:Toast>" +
      "<wp:Text1><string></wp:Text1>" +
      "<wp:Text2><string></wp:Text2>" +
   "</wp:Toast>" +
"</wp:Notification>";

Show:
© 2014 Microsoft. All rights reserved.