// Send a raw HTTP notification to each registered channel URI with the Microsoft Push Notification Server
 public void SendRawNotification(List <Uri> Uris, byte[] Payload, SendNotificationToMPNSCompleted callback)
 {
     foreach (var uri in Uris)
     {
         SendNotificationByType(uri, Payload, NotificationType.Raw, callback);
     }
 }
 private void SendNotificationByType(Uri channelUri, byte[] payload, NotificationType notificationType, SendNotificationToMPNSCompleted callback)
 {
     try
     {
         SendMessage(channelUri, payload, notificationType, callback);
     }
     catch (Exception ex)
     {
         throw;
     }
 }
        private void SendMessage(Uri channelUri, byte[] payload, NotificationType notificationType, SendNotificationToMPNSCompleted callback)
        {
            //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.ContentType = "text/xml; charset=utf-8";
                request.ContentLength = payload.Length;
                request.Headers[MESSAGE_ID_HEADER] = Guid.NewGuid().ToString();
                request.Headers[NOTIFICATION_CLASS_HEADER] = ((int)notificationType).ToString();

                if (notificationType == NotificationType.Toast)
                    request.Headers[WINDOWSPHONE_TARGET_HEADER] = "toast";
                else if (notificationType == NotificationType.Token)
                    request.Headers[WINDOWSPHONE_TARGET_HEADER] = "token";

                request.BeginGetRequestStream((ar) =>
                {
                    //Once async call returns get the Stream object
                    Stream requestStream = request.EndGetRequestStream(ar);

                    //and start to write the payload to the stream asynchronously
                    requestStream.BeginWrite(payload, 0, payload.Length, (iar) =>
                    {
                        //When the writing is done, close the stream
                        requestStream.EndWrite(iar);
                        requestStream.Close();

                        //and switch to receiving the response from MPNS
                        request.BeginGetResponse((iarr) =>
                        {
                            using (WebResponse response = request.EndGetResponse(iarr))
                            {
                                //Notify the caller with the MPNS results
                                OnNotified(notificationType, (HttpWebResponse)response, callback);
                            }
                        },
                        null);
                    },
                    null);
                },
                null);
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    //Notify client on exception
                    OnNotified(notificationType, (HttpWebResponse)ex.Response, callback);
                }

                throw;
            }
        }
 protected void OnNotified(NotificationType notificationType, HttpWebResponse response, SendNotificationToMPNSCompleted callback)
 {
     CallbackArgs args = new CallbackArgs(notificationType, response);
     if (null != callback)
         callback(args);
 }
        public void SendToastNotification(List<Uri> Uris, string message1, string message2, SendNotificationToMPNSCompleted callback)
        {
            byte[] payload = prepareToastPayload(message1, message2);

            foreach (var uri in Uris)
                SendNotificationByType(uri, payload, NotificationType.Toast, callback);
        }
        public void SendTileNotification(List<Uri> Uris, string TokenID, string BackgroundImageUri, string SmallBackgroundImageUri, int Count, string Title, SendNotificationToMPNSCompleted callback)
        {
            byte[] payload = prepareTilePayload(TokenID, BackgroundImageUri, SmallBackgroundImageUri, Count, Title);

            foreach (var uri in Uris)
                SendNotificationByType(uri, payload, NotificationType.Token, callback);
        }
 public void SendRawNotification(List<Uri> Uris, byte[] Payload, SendNotificationToMPNSCompleted callback)
 {
     foreach (var uri in Uris)
         SendNotificationByType(uri, Payload, NotificationType.Raw, callback);
 }
        private void SendMessage(Uri channelUri, byte[] payload, NotificationType notificationType, SendNotificationToMPNSCompleted callback)
        {
            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
            {

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(channelUri);
                request.Method = WebRequestMethods.Http.Post;
                request.ContentType = "text/xml; charset=utf-8";
                request.ContentLength = payload.Length;
                request.Headers[MESSAGE_ID_HEADER] = Guid.NewGuid().ToString();
                request.Headers[NOTIFICATION_CLASS_HEADER] = ((int)notificationType).ToString();

                if (notificationType == NotificationType.Toast)
                    request.Headers[WINDOWSPHONE_TARGET_HEADER] = "toast";
                else if (notificationType == NotificationType.Token)
                    request.Headers[WINDOWSPHONE_TARGET_HEADER] = "token";

                request.BeginGetRequestStream((ar) =>
                {

                    Stream requestStream = request.EndGetRequestStream(ar);

                    requestStream.BeginWrite(payload, 0, payload.Length, (iar) =>
                    {
                        requestStream.EndWrite(iar);
                        requestStream.Close();

                        request.BeginGetResponse((iarr) =>
                        {
                            using (WebResponse response = request.EndGetResponse(iarr))
                            {
                                OnNotified(notificationType, (HttpWebResponse)response, callback);
                            }
                        },
                        null);
                    },
                    null);
                },
                null);
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    OnNotified(notificationType, (HttpWebResponse)ex.Response, callback);
                }

                throw;
            }
        }
        protected void OnNotified(NotificationType notificationType, HttpWebResponse response, SendNotificationToMPNSCompleted callback)
        {
            CallbackArgs args = new CallbackArgs(notificationType, response);

            if (null != callback)
            {
                callback(args);
            }
        }
        private void SendMessage(Uri channelUri, byte[] payload, NotificationType notificationType, SendNotificationToMPNSCompleted callback)
        {
            // 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.ContentType   = "text/xml; charset=utf-8";
                request.ContentLength = payload.Length;
                request.Headers[MESSAGE_ID_HEADER]         = Guid.NewGuid().ToString();
                request.Headers[NOTIFICATION_CLASS_HEADER] = ((int)notificationType).ToString();

                if (notificationType == NotificationType.Toast)
                {
                    request.Headers[WINDOWSPHONE_TARGET_HEADER] = "toast";
                }
                else if (notificationType == NotificationType.Token)
                {
                    request.Headers[WINDOWSPHONE_TARGET_HEADER] = "token";
                }

                request.BeginGetRequestStream((ar) =>
                {
                    // Once async call returns get the Stream object
                    Stream requestStream = request.EndGetRequestStream(ar);

                    // Start to write the payload to the stream asynchronously
                    requestStream.BeginWrite(payload, 0, payload.Length, (iar) =>
                    {
                        // When the writing is done, close the stream
                        requestStream.EndWrite(iar);
                        requestStream.Close();

                        // Switch to receiving the response from MPNS
                        request.BeginGetResponse((iarr) =>
                        {
                            using (WebResponse response = request.EndGetResponse(iarr))
                            {
                                //Notify the caller with the Microsoft Push Notification Server results
                                OnNotified(notificationType, (HttpWebResponse)response, callback);
                            }
                        },
                                                 null);
                    },
                                             null);
                },
                                              null);
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                {
                    // Notify the caller on exception
                    OnNotified(notificationType, (HttpWebResponse)ex.Response, callback);
                }

                throw;
            }
        }
 // Send a notification to a registered channel URI with the Microsoft Push Notification Server
 private void SendNotificationByType(Uri channelUri, byte[] payload, NotificationType notificationType, SendNotificationToMPNSCompleted callback)
 {
     try
     {
         SendMessage(channelUri, payload, notificationType, callback);
     }
     catch (Exception ex)
     {
         throw;
     }
 }
        // Send a live tile notification to each registered channel URI with the Microsoft Push Notification Server
        public void SendTileNotification(List <Uri> Uris, string TokenID, int Count, string Title, SendNotificationToMPNSCompleted callback)
        {
            byte[] payload = prepareTilePayload(TokenID, Count, Title);

            foreach (var uri in Uris)
            {
                SendNotificationByType(uri, payload, NotificationType.Token, callback);
            }
        }
        // Send a toast notification to each registered channel URI with the Microsoft Push Notification Server
        public void SendToastNotification(List <Uri> Uris, string message1, string message2, SendNotificationToMPNSCompleted callback)
        {
            byte[] payload = prepareToastPayload(message1, message2);

            foreach (var uri in Uris)
            {
                SendNotificationByType(uri, payload, NotificationType.Toast, callback);
            }
        }