Exemple #1
0
        /// <summary>
        /// Callback that asynchronously updates the UI with the server's response or error message.
        /// </summary>
        private void PushSender_NotificationSendCompleted(PushNotificationCallbackArgs args)
        {
            string text = "Status Code: " + args.StatusCode.ToString() + Environment.NewLine +
                          "TimeStamp: " + args.Timestamp.ToString() + Environment.NewLine +
                          "Notification Type: " + args.NotificationType.ToString() + Environment.NewLine +
                          "Notification Status: " + args.NotificationStatus + Environment.NewLine +
                          "Device Status: " + args.DeviceConnectionStatus + Environment.NewLine +
                          "Subscription Status: " + args.DeviceConnectionStatus;

            if (serverResponse.InvokeRequired)
            {
                serverResponse.Invoke(new UpdateServerResponseDelegate(UpdateServerResponse), new object[] { text });
            }
            else
            {
                UpdateServerResponse(text);
            }
        }
Exemple #2
0
        /// <summary>
        /// helper function to set up the request headers based on type and send the notification payload.
        /// </summary>
        private void SendNotificationByType(Uri channelUri, byte[] payload, NotificationType notificationType)
        {
            // Check the length of the payload and reject it if too long.
            if (payload.Length > MAX_PAYLOAD_LENGTH)
            {
                throw new ArgumentOutOfRangeException("Payload is too long. Maximum payload size shouldn't exceed " + MAX_PAYLOAD_LENGTH.ToString() + " bytes");
            }

            try
            {
                // Create and initialize the request object.
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(channelUri);
                request.Method        = WebRequestMethods.Http.Post;
                request.ContentLength = payload.Length;
                request.Headers[MESSAGE_ID_HEADER] = Guid.NewGuid().ToString();

                // Each type of push notification uses a different code in its X-NotificationClass
                // header to specify its delivery priority.  The three priorities are:

                //     Realtime.  The notification is delivered as soon as possible.
                //     Priority.  The notification is delivered within 450 seconds.
                //     Regular.   The notification is delivered within 900 seconds.

                //	      Realtime    Priority    Regular
                // Raw    3-10        13-20       23-31
                // Tile   1           11          21
                // Toast  2           12          22

                switch (notificationType)
                {
                case NotificationType.Tile:
                    // the notification type for a tile notification is "token".
                    request.Headers[WINDOWSPHONE_TARGET_HEADER] = "token";
                    request.ContentType = "text/xml";
                    // Request real-time delivery for tile notifications.
                    request.Headers[NOTIFICATION_CLASS_HEADER] = "1";
                    break;

                case NotificationType.Toast:
                    request.Headers[WINDOWSPHONE_TARGET_HEADER] = "toast";
                    request.ContentType = "text/xml";
                    // Request real-time delivery for toast notifications.
                    request.Headers[NOTIFICATION_CLASS_HEADER] = "2";
                    break;

                case NotificationType.Raw:
                    // Request real-time delivery for raw notifications.
                    request.Headers[NOTIFICATION_CLASS_HEADER] = "3";
                    break;

                default:
                    throw new ArgumentException("Unknown notification type", "notificationType");
                }

                Stream requestStream = request.GetRequestStream();

                requestStream.Write(payload, 0, payload.Length);
                requestStream.Close();

                HttpWebResponse response = request.GetResponse() as HttpWebResponse;

                if (NotificationSendCompleted != null && response != null)
                {
                    PushNotificationCallbackArgs args = new PushNotificationCallbackArgs(notificationType, (HttpWebResponse)response);
                    NotificationSendCompleted(args);
                }
            }
            catch (WebException ex)
            {
                // Notify the caller on exception as well.
                if (NotificationSendCompleted != null)
                {
                    if (null != ex.Response)
                    {
                        PushNotificationCallbackArgs args = new PushNotificationCallbackArgs(notificationType, (HttpWebResponse)ex.Response);
                        NotificationSendCompleted(args);
                    }
                }
            }
        }