如何设置 Windows Phone 的回调注册请求

2013/3/11

适用于: Windows Phone 8 | Windows Phone OS 7.1

如果您的云服务已经过身份验证,则可以设置一个将回调 URI 和消息与订阅关联的回调注册请求。注册回调之后,如果设备过渡到非活动状态,则 Microsoft 推送通知服务会将此消息发送到回调 URI。设备返回活动状态之后,此云服务应该重新注册该回调。

对于每个订阅只能注册一个回调。如果您的云服务尝试多次注册一个回调,则将只保留最后一个回调的数据。

您的负载必须包含触发与订阅关联的已注册事件时将推送到云服务的内容。当前,将触发回调通知的唯一事件发生在设备更改为非活动状态时。Microsoft 推送通知服务将此负载信息与订阅存储在一起。负载可以由文本或二进制数据组成,最大大小为 1 KB。

发送回调注册请求

以下代码示例介绍如何发送回调注册请求。

// The notification channel URI that the Push Notification Service returns to the app. 
// The following value is an example URI for an authenticated cloud service.
string notificationUri = "https://notify.mpnf1.live-int.com/unthrottledthirdparty/01.00/AAEecz4fHY3eRoEfxiI6WayjAgAAAAADAQAAAAQUZm52Ojk0MzAwOUIxRUExQkQ2ODY";

// The cloud service must append "/callback" to the notification channel URI.  
string mpnCallbackServiceUri = notificationUri + "/callback";

// The URI to post the set callback request to.
string callbackUri = "https://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 cloud 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");

另请参见

其他资源

发送 Windows Phone 的推送通知

设置已验证的 Web 服务以发送 Windows Phone 的推送通知