Windows Phone 用のコールバック登録要求を設定する方法

2013/03/11

対象: Windows Phone 8 | Windows Phone OS 7.1

クラウド サービスが認証されていると、コールバック URI およびメッセージをサブスクリプションに関連付けるコールバック登録要求を設定できます。コールバックが登録された後、デバイスが非アクティブ状態に移行すると、Microsoft Push Notification Service によってコールバック URI にメッセージが送信されます。デバイスがアクティブ状態に戻った後、クラウド サービスでコールバックを再登録する必要があります。

各サブスクリプションに登録できるコールバックは 1 つだけです。クラウド サービスでコールバックを 2 回以上登録しようとすると、最後のコールバックのデータだけが維持されます。

ペイロードには、サブスクリプションに関連付けられた登録済みイベントがトリガーされたときにクラウド サービスにプッシュされるコンテンツが含まれている必要があります。現在、コールバック通知がトリガーされるイベントは、デバイスが非アクティブ状態に変わるイベントだけです。Microsoft Push Notification Service には、このペイロード情報がサブスクリプションと共に格納されています。ペイロードは、テキストまたはバイナリのデータで構成され、サイズは最大で 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 のプッシュ通知の送信

Windows Phone のプッシュ通知を送信するように認証済み Web サービスを設定する