using System;
using System.IO;
using System.Net;
using System.Text;

namespace CAPushTile
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://db3.notify.live.net/throttledthirdparty/01.00/AAEDHXtMf6OSRo9vQnMJxa5tAgAAAAADBQAAAAQUZm52OjIzOEQ2NDJDRkI5MEVFMEQ";
            string data = string.Format(
                "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<wp:Notification xmlns:wp=\"WPNotification\">" +
                     "<wp:Tile>" +
                     "<wp:BackgroundImage></wp:BackgroundImage>" +
                     "<wp:Count>{0}</wp:Count>" +
                     "<wp:Title>{1}</wp:Title>" +
                     "</wp:Tile>" +
                "</wp:Notification>",

                "3", "Neue Nachrichten");

            //Erstellen eines neuen Webrequest mit der vom Gerät empfangenen Adresse
            WebRequest request = WebRequest.Create(url);

            request.Headers.Add("X-WindowsPhone-Target", "token");
            request.Headers.Add("X-NotificationClass", "1");

            //Es werden Benachrichtigungen immer nur im XML Format versand
            request.ContentType = "text/xml";
            request.Method = "POST";

            //Schreiben der Nachricht in den Request-Stream
            byte[] bytes = Encoding.UTF8.GetBytes(data);

            request.ContentLength = bytes.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytes, 0, bytes.Length);

                try
                {
                    //Auslesen des Antwortstatus
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    string notificationStatus = response.Headers["X-NotificationStatus"];
                    string subscriptionStatus = response.Headers["X-SubscriptionStatus"];
                    string connectionStatus = response.Headers["X-DeviceConnectionStatus"];

                    Console.Write(
                        String.Format("X-NotificationStatus:{0}\r\nX-SubscriptionStatus:{1}\r\nX-DeviceConnectionStatus:{2}",
                        notificationStatus, subscriptionStatus, connectionStatus));
                    Console.ReadKey();
                }
                catch (WebException ex)
                {
                    Console.Write("WebException occured with Statuscode:" +
                        ((HttpWebResponse)ex.Response).StatusCode.ToString());
                }
            }
        }
    }
}
