private byte[] GetTilePayload(NotificationData data)
        {
            string payload = string.Format(
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<wp:Notification xmlns:wp=\"WPNotification\">" +
                "<wp:Tile" +
                (!data.UpdateAppTile ? ">" : "Id=\"/MainPage.xaml?id=" + data.PersonName + "\">") +
                "<wp:BackgroundImage>{2}</wp:BackgroundImage>" +
                "<wp:Count>{1}</wp:Count>" +
                "<wp:Title>{0}</wp:Title>" +
                "<wp:BackBackgroundImage>{3}</wp:BackBackgroundImage>" +
                "<wp:BackTitle>{4}</wp:BackTitle>" +
                "<wp:BackContent>{5}</wp:BackContent>" +
                "</wp:Tile> " +
                "</wp:Notification>",
                data.Title, data.Count, data.TileUri, data.BackTileUri, data.BackTitle, data.BackContent);

            return Encoding.UTF8.GetBytes(payload);
        }
        public List<string> PushToSubscribedPhones(NotificationData data)
        {
            using (EmergencyDBEntities context = new EmergencyDBEntities())
            {
                foreach (var entry in context.Devices)
                {
                    if (data.PhoneId.ToString() == entry.DeviceID)
                    {
                        Uri uri = new Uri(entry.DeviceUrl);
                        byte[] payload = new byte[0];

                        var request = (HttpWebRequest)WebRequest.Create(uri);
                        request.Method = "POST";
                        request.ContentType = "text/xml";

                        if (data.PushType == "toast")
                        {
                            payload = GetToastPayload(data.Title, data.PersonName);
                            request.Headers.Add("X-WindowsPhone-Target", "toast");
                            request.Headers.Add("X-NotificationClass", "2");
                        }
                        else if (data.PushType == "tile")
                        {
                            payload = GetTilePayload(data);
                            request.Headers.Add("X-WindowsPhone-Target", "token");
                            request.Headers.Add("X-NotificationClass", "1");
                        }
                        else if (data.PushType == "raw")
                        {
                            payload = Encoding.UTF8.GetBytes(data.Title);
                            request.Headers.Add("X-NotificationClass", "3");
                        }

                        request.ContentLength = payload.Length;

                        using (Stream requestStream = request.GetRequestStream())
                        {
                            requestStream.Write(payload, 0, payload.Length);
                        }

                        try
                        {
                            var response = (HttpWebResponse)request.GetResponse();
                            data.NotificationStatus = response.Headers["X-NotificationStatus"];
                            data.NotificationChannelStatus = response.Headers["X-SubscriptionStatus"];
                            data.DeviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
                        }
                        catch (Exception ex) { return new List<string> { "Device is not connected!", ex.Message }; }
                    }
                }
            }
            return new List<string> { data.NotificationStatus, data.NotificationChannelStatus, data.DeviceConnectionStatus };
        }