+91 20 24210201/301 info@genex.co.in

Push notifications for Windows Phone 8

It is very easy to send push notification on Windows Phone 8. Just doing few steps.

Creating a push client to receive toast notifications:
1) Create a new Windows Phone 8 app.

2) Name the project GenexToastClient.

3) Add the following using directives to the top of the MainPage.xaml.cs file.

  using Microsoft.Phone.Notification;

3) Add following code in MainPage.xaml.cs file.

 public MainPage()
      HttpNotificationChannel pushChannel;
      string channelName = "ToastDemo";
      pushChannel = HttpNotificationChannel.Find(channelName);
     if (pushChannel == null)
            pushChannel = new HttpNotificationChannel(channelName);
            pushChannel.ChannelUriUpdated += new EventHandler(PushChannel_ChannelUriUpdated);
            pushChannel.ChannelUriUpdated += new EventHandler(PushChannel_ChannelUriUpdated);
            MessageBox.Show(String.Format("Channel Uri is {0}", pushChannel.ChannelUri.ToString()));

    void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)

            Dispatcher.BeginInvoke(() =>
                //Normally, the URI would be passed back to your web service at this point.

                MessageBox.Show(String.Format("Channel Uri is {0}", e.ChannelUri.ToString()));


Sending a Toast Notification:
1) Create new ASP.Net web application

Add one button on default.aspx page  and put the following code in side the button_onclick event
//Run GenexToastClient app. Windows Phone Emulator initializes, and then the app starts. After a moment or two,
//the app should display a message with the push channel URI.  This URI also will be displayed in the Visual Studio debugger Output window.
//copy this URI and pass to ChannelUri variable in following code.

string ChannelUri = "http://db3.notify.live.net/throttledthirdparty/01.00/AQGshG_QxJs2QIO1OID6wTLxAgAAAAADAQAAAAQUZm52OkJCMjg1QTg1QkZDMkUxREQFBkVVTk8wMQ";

                HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(ChannelUri);
                sendNotificationRequest.Method = "POST";


              // Set the notification payload to send.
                byte[] notificationMessage = Encoding.Default.GetBytes(toastMessage);

                // Set the web request content length.
                sendNotificationRequest.ContentLength = notificationMessage.Length;
                sendNotificationRequest.ContentType = "text/xml";
                sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
                sendNotificationRequest.Headers.Add("X-NotificationClass", "2");

                using (Stream requestStream = sendNotificationRequest.GetRequestStream())
                    requestStream.Write(notificationMessage, 0, notificationMessage.Length);

                // Send the notification and get the response.
                HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
                string notificationStatus = response.Headers["X-NotificationStatus"];
                string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
                string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];

3) Run web app and  Press the button on default.aspx page. you will get the notification on your windows phone.

Happy Coding   smiley
Comments (0)

Post a comment

Maximum 250 alphanumerals only