How to: Set up a Callback Registration Request for Windows Phone
March 22, 2012
If your web service is authenticated, you can set up a callback registration request that associates a callback URI and message with a subscription. After a callback is registered, if the device transitions into an inactive state, the Microsoft Push Notification Service will send the message to the callback URI. After the device returns to an active state, the web service should re-register the callback.
Only one callback can be registered for each subscription. If your web service tries to register a callback multiple times, only the data from the last callback will be maintained.
Your payload must contain the content that will be pushed to the web service when a registered event associated with the subscription is triggered. Currently, the only event that will trigger a callback notification is when the device changes to an inactive state. The Microsoft Push Notification Service stores this payload information with the subscription. The payload can comprise either text or binary data, with a maximum size of 1 KB.
The following code sample describes how to send a callback registration request.
// The notification channel URI that the Push Notification Service returns to the application. // The following value is an example URI for an authenticated web service. string notificationUri = "https://notify.mpnf1.live-int.com/unthrottledthirdparty/01.00/AAEecz4fHY3eRoEfxiI6WayjAgAAAAADAQAAAAQUZm52Ojk0MzAwOUIxRUExQkQ2ODY"; // The web service must append "/callback" to the notification channel URI. string mpnCallbackServiceUri = notificationUri + "/callback"; // The URI to post the set callback request to. string callbackUri = "http://www.contoso.com"; // Sets the callback message payload to send to the Push Notification Service. It is an opaque blob that the Push Notification Service posts to the provided callback URI // when an associated event triggers. The maximum allowed size for this payload is 1 KB. byte[] callbackMessage = new byte[1024]; // Create a web request to send the register callback request to the Push Notification Service. HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(mpnCallbackServiceUri); // A mandatory custom header that specifies the URI to post the callback message to when any registered event with the associated subscription triggers. webRequest.Headers.Add("X-CallbackURI", callbackUri); // HTTP POST is the only allowed method to send the notification. webRequest.Method = "POST"; // Sets the web request content length. webRequest.ContentLength = callbackMessage.Length; // Set the content type according to the type of callback message being sent. webRequest.ContentType = "application/*"; using (Stream requestStream = webRequest.GetRequestStream()) { requestStream.Write(callbackMessage, 0, callbackMessage.Length); } // Get the response. This example gets the web response synchronously, but a web service can get it asynchronously as well. HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); // Ensure that the 'HTTP 200 OK' response is received from the Push Notification Service. Debug.Assert(response.StatusCode == HttpStatusCode.OK); // You can get the status of the notification channel associated with the callback request // using the custom headers in the received response. string subscriptionStatus = response.Headers.Get("X-SubscriptionStatus"); // You can get the device connection status of the notification channel associated with the callback request // using the custom headers in the received response. string deviceConnectionStatus = response.Headers.Get("X-DeviceConnectionStatus");