コード例 #1
0
 /// <summary>
 /// Enques all types of WP7 message to each device URI in the subscription.
 /// </summary>
 /// <param name="ddm">Device to send message to.</param>
 /// <param name="messageBytes">Message bytes of the message to send.</param>
 /// <param name="type">Type of message to send.</param>
 private void EnqueWP7Notification(DeviceDataModel ddm, byte[] messageBytes, WP7NotificationType type)
 {
     // Get the all devices that have signed up for the subscription
     // Send the message to each device addrress or URI
     if (Uri.IsWellFormedUriString(ddm.Address, UriKind.Absolute))
     {
         this.SendWP7Message(new Uri(ddm.Address), messageBytes, type);
     }
     else
     {
         if (this.DeviceIdFormatError != null)
         {
             this.DeviceIdFormatError(this, new NotificationEventArgs(new DeviceIdFormatException(ddm.Address)));
         }
     }
 }
コード例 #2
0
        /// <summary>
        /// Send the message out to the device URI.
        /// </summary>
        /// <param name="uri">URI of the device to send message to.</param>
        /// <param name="messageBytes">Bytes of the message.</param>
        /// <param name="notificationType">Type of the notification - Tile, Toast or Raw.</param>
        private void SendWP7Message(Uri uri, byte[] messageBytes, WP7NotificationType notificationType)
        {
            var request = (HttpWebRequest)WebRequest.Create(uri);

            request.Method        = WebRequestMethods.Http.Post;
            request.ContentType   = "text/xml";
            request.ContentLength = messageBytes.Length;
            request.Headers.Add("X-MessageID", Guid.NewGuid().ToString());

            // If the cloud service sending this request is authenticated, it needs to send its certificate.
            // Otherwise, this step is not needed.
            // if (_x509Certificate != null)
            //    request.ClientCertificates.Add(_x509Certificate);
            switch (notificationType)
            {
            case WP7NotificationType.Toast:
                request.Headers["X-WindowsPhone-Target"] = "toast";
                request.Headers.Add("X-NotificationClass", ((int)this.ToastInterval).ToString(CultureInfo.InvariantCulture));
                break;

            case WP7NotificationType.Tile:
                request.Headers["X-WindowsPhone-Target"] = "token";
                request.Headers.Add("X-NotificationClass", ((int)this.TileInterval).ToString(CultureInfo.InvariantCulture));
                break;

            case WP7NotificationType.Raw:
                request.Headers.Add("X-NotificationClass", ((int)this.RawInterval).ToString(CultureInfo.InvariantCulture));
                break;
            }

            bool            sent  = false;
            int             tries = 0;
            HttpWebResponse response;

            while (!sent && tries < this.retries)
            {
                try
                {
                    using (var requestStream = request.GetRequestStream())
                    {
                        // we are not using retries yet.. or looking for errors here. We should log errors too.
                        requestStream.Write(messageBytes, 0, messageBytes.Length);
                    }

                    // Sends the notification and gets the response.
                    response = (HttpWebResponse)request.GetResponse();
                    string notificationStatus        = response.Headers["X-NotificationStatus"];
                    string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
                    string deviceConnectionStatus    = response.Headers["X-DeviceConnectionStatus"];
                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        return;
                    }

                    if (deviceConnectionStatus == "TempDisconnected" || notificationStatus == "QueueFull")
                    {
                        tries++;
                        if (tries > this.retries)
                        {
                            if (this.NotificationFailed != null)
                            {
                                this.NotificationFailed(this, new NotificationEventArgs(new NotificationException(string.Format(CultureInfo.InvariantCulture, "{0} notification failures for {1}. Giving up", this.retries, uri.OriginalString))));
                            }

                            return;
                        }
                    }
                    else
                    {
                        sent = true;
                    }
                }
                catch (WebException e)
                {
                    // Check why we got the exception
                    if (this.NotificationFailed != null)
                    {
                        this.NotificationFailed(this, new NotificationEventArgs(new NotificationException("Notification failed with exception " + e.Message + "for " + uri.OriginalString, e)));
                    }

                    throw;
                }
            }
        }
コード例 #3
0
 private void EnqueWP7Notification(IEnumerable <DeviceDataModel> ddmList, byte[] messageBytes, WP7NotificationType type)
 {
     // Get the all devices that have signed up for the subscription
     // Send the message to each device addrress or URI
     foreach (DeviceDataModel ddm in ddmList)
     {
         if (Uri.IsWellFormedUriString(ddm.Address, UriKind.Absolute))
         {
             try
             {
                 this.SendWP7Message(new Uri(ddm.Address), messageBytes, type);
             }
             catch (WebException e)
             {
                 HttpWebResponse wr = e.Response as HttpWebResponse;
                 if (wr.StatusCode == HttpStatusCode.NotFound)
                 {
                     this.sds.DeleteDeviceSubscriptions(ddm);
                 }
             }
         }
         else
         {
             if (this.DeviceIdFormatError != null)
             {
                 this.DeviceIdFormatError(this, new NotificationEventArgs(new DeviceIdFormatException(ddm.Address)));
             }
         }
     }
 }
コード例 #4
0
        /// <summary>
        /// Send the message out to the device URI.
        /// </summary>
        /// <param name="uri">URI of the device to send message to.</param>
        /// <param name="messageBytes">Bytes of the message.</param>
        /// <param name="notificationType">Type of the notification - Tile, Toast or Raw.</param>
        private void SendWP7Message(Uri uri, byte[] messageBytes, WP7NotificationType notificationType)
        {
            var request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = WebRequestMethods.Http.Post;
            request.ContentType = "text/xml";
            request.ContentLength = messageBytes.Length;
            request.Headers.Add("X-MessageID", Guid.NewGuid().ToString());

            // If the cloud service sending this request is authenticated, it needs to send its certificate.  
            // Otherwise, this step is not needed.  
            // if (_x509Certificate != null)
            //    request.ClientCertificates.Add(_x509Certificate);  
            switch (notificationType)
            {
                case WP7NotificationType.Toast:
                    request.Headers["X-WindowsPhone-Target"] = "toast";
                    request.Headers.Add("X-NotificationClass", ((int)this.ToastInterval).ToString(CultureInfo.InvariantCulture));
                    break;
                case WP7NotificationType.Tile:
                    request.Headers["X-WindowsPhone-Target"] = "token";
                    request.Headers.Add("X-NotificationClass", ((int)this.TileInterval).ToString(CultureInfo.InvariantCulture));
                    break;
                case WP7NotificationType.Raw:
                    request.Headers.Add("X-NotificationClass", ((int)this.RawInterval).ToString(CultureInfo.InvariantCulture));
                    break;
            }

            bool sent = false;
            int tries = 0;
            HttpWebResponse response;

            while (!sent && tries < this.retries)
            {
                try
                {
                    using (var requestStream = request.GetRequestStream())
                    {
                        // we are not using retries yet.. or looking for errors here. We should log errors too.
                        requestStream.Write(messageBytes, 0, messageBytes.Length);
                    }

                    // Sends the notification and gets the response.
                    response = (HttpWebResponse)request.GetResponse();
                    string notificationStatus = response.Headers["X-NotificationStatus"];
                    string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
                    string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        return;
                    }

                    if (deviceConnectionStatus == "TempDisconnected" || notificationStatus == "QueueFull")
                    {
                        tries++;
                        if (tries > this.retries)
                        {
                            if (this.NotificationFailed != null)
                            {
                                this.NotificationFailed(this, new NotificationEventArgs(new NotificationException(string.Format(CultureInfo.InvariantCulture, "{0} notification failures for {1}. Giving up", this.retries, uri.OriginalString))));
                            }

                            return;
                        }
                    }
                    else
                    {
                        sent = true;
                    }
                }
                catch (WebException e)
                {
                    // Check why we got the exception
                    if (this.NotificationFailed != null)
                    {
                        this.NotificationFailed(this, new NotificationEventArgs(new NotificationException("Notification failed with exception " + e.Message + "for " + uri.OriginalString, e)));
                    }

                    throw;
                }
            }
        }
コード例 #5
0
 private void EnqueWP7Notification(IEnumerable<DeviceDataModel> ddmList, byte[] messageBytes, WP7NotificationType type)
 {
     // Get the all devices that have signed up for the subscription
     // Send the message to each device addrress or URI
     foreach (DeviceDataModel ddm in ddmList)
     {
         if (Uri.IsWellFormedUriString(ddm.Address, UriKind.Absolute))
         {
             try
             {
                 this.SendWP7Message(new Uri(ddm.Address), messageBytes, type);
             }
             catch (WebException e)
             {
                 HttpWebResponse wr = e.Response as HttpWebResponse;
                 if (wr.StatusCode == HttpStatusCode.NotFound)
                 {
                     this.sds.DeleteDeviceSubscriptions(ddm);
                 }
             }
         }
         else
         {
             if (this.DeviceIdFormatError != null)
             {
                 this.DeviceIdFormatError(this, new NotificationEventArgs(new DeviceIdFormatException(ddm.Address)));
             }
         }
     }
 }
コード例 #6
0
 /// <summary>
 /// Enques all types of WP7 message to each device URI in the subscription.       
 /// </summary>
 /// <param name="ddm">Device to send message to.</param>
 /// <param name="messageBytes">Message bytes of the message to send.</param>
 /// <param name="type">Type of message to send.</param>
 private void EnqueWP7Notification(DeviceDataModel ddm, byte[] messageBytes, WP7NotificationType type)
 {
     // Get the all devices that have signed up for the subscription
     // Send the message to each device addrress or URI
     if (Uri.IsWellFormedUriString(ddm.Address, UriKind.Absolute))
     {
         this.SendWP7Message(new Uri(ddm.Address), messageBytes, type);
     }
     else
     {
         if (this.DeviceIdFormatError != null)
         {
             this.DeviceIdFormatError(this, new NotificationEventArgs(new DeviceIdFormatException(ddm.Address)));
         }
     }
 }