(X) Hide this
    • Login
    • Join
      • Say No Bots Generate New Image
        By clicking 'Register' you accept the terms of use .

Implementing Push Notifications in Windows Phone 7 - UPDATED (April CTP)

(4 votes)
Zoltan Arvai
>
Zoltan Arvai
Joined Jul 20, 2009
Articles:   7
Comments:   5
More Articles
25 comments   /   posted on Apr 12, 2010
Categories:   Windows Phone

This article is written for earlier version of Silverlight for Windows Phone 7 and may not be fully compatible with the latest version.

Why is multitasking missing and how to hide the fact?

Windows Phone 7 at this very moment does not support Multitasking for 3rd party applications. There are a lot of reasons for that and as an IPhone user I could not agree more with this decision. Running different applications parallel means more power consumption, more memory consumption and higher cpu load. These factors directly lead to reduced User Experience, like your battery says goodbye in a couple of hours, the screen is not so smooth when using multitouch, it’s „lagging”, or changing between applications might get really slow. No matter how evolved are today’s smartphone hardwares, these devices are not PCs at this very moment. On reduced performance capabilities, you expect lot more innovative applications, higher user experience, more sci-fi like user interfaces than on a PC . You want high definition media, real-“time-ish” connection to you social networks, to your social “identity”. So sacrifices and compromises must be made, so Microsoft had to sacrifice Multitasking in favor of higher user experience and performance. The end user is the king! The end user is not me and it’s probably not you. Microsoft introduced the end users to us. They were Anna and Miles :) I am not Miles and definitely not Anna :) They are two personas created by Microsoft to represent the target market for this phone. While you and I might miss Multitasking Anna and Miles don’t know what multitasking is. You know what? They don’t want to know! Microsoft does a pretty could job to hide the lack of this feature and provides different workarounds to make Anna and Miles think, that they can do many things with their phone at the same time. So how can they miss something, if to them it seems like it’s there?

The Push Notification Experience

To hide the lack of multitasking Microsoft uses different concepts to simulate parallel-ish behavior. One of these is the Push Notification feature.

Okay, what is Push Notification? For Anna and Miles Push Notification means that their applications can tell them if there is something new, there is something interesting, even if the application is not in the foreground! They just tap on the notification and the application comes up and shows whatever is necessary.

How push notification works

For you and me it’s really different. The Push Notification feature consits of many components.

  1. The Windows Phone application
  2. Custom (cloud)service to provide the data
  3. Push Notification Service
  4. Push Client

So your Windows Phone 7 application get’s the data from a service. But what happens if your application dies? (e.g. gets wiped out of memory) It won’t have to chance to poll your service for more data. (which would consume a lot of resources by the way).

  1. So your WP7 application has the opportunity to open up an HttpChannel and tell the Push Notification Service that it wants to accept notifications after its lifetime.
  2. Your WP7 application tells the Push Client, built into your phone, where to display the message when it arrives.
  3. The Push Notification Service returns a URL for your application.
  4. The Windows Phone 7 application now can send this url to your custom service, that has your data.
  5. Your custom service can send information to the Push Notification Service using this url.
  6. The Push Notification Service identifies your phone by this url and pushes down the data to the Push Client, built into your phone
  7. The Push Client transfers the message where it was told to.
  8. Tapping on the message opens up the application.

It’s simple as that!

IC386770[1]

The Push Notification Service pushes the messages arrived from different source to the push client in packages in a timely fashion. Please note that the service DOES NOT guarantee message delivery. Which means that your message probably arrives but you cannot count on it! So you should use push notification only in scenarios where losing a message is not critical.

Push Notification Types

Windows Phone 7 support 3 types of notification:

  1. Raw Notification
    In this case your application runs and you get a push notification while running.
  2. Toast Notification
    In this case your application might or might not run but the message will be displayed anyway as a toast message.
  3. Tile Notification
    If you pin down your application on the main screen, you can tell the shell that when a notification arrives change to picture (the tile) on the main screen. In this case your application is not running.

Okay but how to implement that?

Implementing a Toast Notification

Preparing your phone to accept push notifications is very easy.

Step 1

You have to setup an HttpNotificationChannel to get the URL and you have to pass that to your custom (cloud) service.

 private void SetupChannel()
 {
     HttpNotificationChannel httpChannel = null;
     string channelName = "DemoChannel";
  
     try
     {
         Debug.WriteLine("Creating Channel");
         httpChannel = new HttpNotificationChannel(channelName);
   
         //When we get the URI (which might change anytime)
         httpChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(httpChannel_ChannelUriUpdated);
   
         //When something bad happens
         httpChannel.ExceptionOccurred += new EventHandler<NotificationChannelExceptionEventArgs>(httpChannel_ExceptionOccurred);
  
         Debug.WriteLine("Opening Channel");
  
         //Tell push client to register us for push notification
         httpChannel.Open();
  
         Debug.WriteLine("Binding to Shell, (Toast)");
  
         //Register for a Notification type
         BindToShell(httpChannel);
     }
     catch (NotificationChannelExistsException)
     {
         Debug.WriteLine("Channel Exists, retrieving existing channel");
         httpChannel = HttpNotificationChannel.Find(channelName);
  
         //If we can't get it, then close and reopen it.
         if (httpChannel.ChannelUri == null)
         {
             Debug.WriteLine("Cannot retrieve channel, retrying...");
             httpChannel.UnbindToShellEntryPoint();
             httpChannel.Close();
  
             //Re-try
             SetupChannel();
             return;
         }
         else
         {
             ChannelUri = httpChannel.ChannelUri;
             Debug.WriteLine("Channel retrieved: " + httpChannel.ChannelUri);
             //Make sure, binding to shell exists
             BindToShell(httpChannel);
         }
     }
     catch (NotificationChannelOpenException)
     {
         Debug.WriteLine("Channel is already open");
         httpChannel = HttpNotificationChannel.Find(channelName);
         ChannelUri = httpChannel.ChannelUri;
         //Make sure, binding to shell exists
         BindToShell(httpChannel);
     }
 }
  
 private static void BindToShell(HttpNotificationChannel httpChannel)
 {
     //This is a toast notification
     try
     {
         httpChannel.BindToShellNotification();
     }
     catch (NotificationChannelBindingExistsException)
     {
     }
  
     //This is a tile notification
     //ShellEntryPoint Tile = new ShellEntryPoint();
     //Tile.RemoteImageUri = new Uri("http://shared.live.com/7E81kqTseEOmzDlpeFPS8g/Web/images/grouplogo.png");
     //httpChannel.BindToShellEntryPoint(Tile);
 }
  
 void httpChannel_ExceptionOccurred(object sender, NotificationChannelExceptionEventArgs e)
 {
     //Display Message
     Dispatcher.BeginInvoke(() =>
     {
         txtError.Text = e.Exception.Message;
     });
 }
  
  
 void httpChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
 {
     //We get the new Uri (or maybe it's updated)
     ChannelUri = e.ChannelUri;
     
    //Now pass this Uri to your custom service. 
     //It should now where to send the data
     //....
 }

Step 2

We have to tell the push client what kind of notification we’re expecting. So let’s setup a toast notification:

httpChannel.BindToShellNotification();

This was easy, wasn’t it :)

I have to mention that you really need to get ready for exceptions since the HttpNotificationChannel and the Open() method really loves to throw all kinds of exceptions like NotificationChannelExistsException or NotificationChannelOpenException. If your channel is already open and did not close before, you can get the instance by calling HttpNotificationChannel.Find(string channelName).

In the previous code sections we mentioned that the channel url should be sent to our custom service.

Step 3

So we are ready to accept Push Notifications. But how can we send them? You have to compose HTTP Messages and send them to the url you received from the phone!

 HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);
 sendNotificationRequest.Method = "POST";
  
 //Indicate that you'll send toast notifications!
 sendNotificationRequest.ContentType = "text/xml";
 sendNotificationRequest.Headers = new WebHeaderCollection();
 sendNotificationRequest.Headers.Add("X-NotificationClass", "2");
  
 if (string.IsNullOrEmpty(txtMessage.Text)) return;
   
 //Create xml envelope
 string data = "X-WindowsPhone-Target: toast\r\n\r\n" +
                     "<?xml version='1.0' encoding='utf-8'?>" +
                     "<wp:Notification xmlns:wp='WPNotification'>" +
                         "<wp:Toast>" +
                             "<wp:Text1>{0}</wp:Text1>" +
                         "</wp:Toast>" +
                     "</wp:Notification>";
  
 //Wrap custom data into envelope
 string message = string.Format(data, txtMessage.Text);
 byte[] notificationMessage = Encoding.Default.GetBytes(message);
  
  
 // Set Content Length
 sendNotificationRequest.ContentLength = notificationMessage.Length;
  
 //Push data to stream
 using (Stream requestStream = sendNotificationRequest.GetRequestStream())
 {
     requestStream.Write(notificationMessage, 0, notificationMessage.Length);
 }
   
  
 //Get reponse for message sending
 HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
 string notificationStatus = response.Headers["X-NotificationStatus"];
 string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
 string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];

Okay this is just a little bit more complex. Personally I’d prefer to write a couple of wrapper classes so the developer doesn’t have to do this. So we created and HttpWebRequest with Post Method type. We add an X-NotificationClass header with the value of 2 which stands for Toast Notifications and (real-time) batching intervals. Next we create an xml envelope-like string to wrap our message. We push our wrapped message into the request stream. We send the request and wait for the answer. The answer is very important because we have to know what’s up with our phone! Is it still connected? Is it still listening? If not, then we should stop sending messages to this url.

push

Step 4

Make sure, that in the WMAppManifest.xml, the App element has a publisher attribute, and it is set to some constant string. Otherwise you may get a lot of NofiticationChannelExistsException. (Not sure why, and why this is not documented)

Summary

So basically this is it. It’s really not complicated to work with push notification. Most importantly it helps to make the user think that his application is still active even if its not in the foreground. It’s definitely not multitasking and push notification won’t help you make more battery power. But it’s still less expensive than doing actual multitasking. Of course by itself, Push Notification doesn’t replace multitasking or solve the problem, but it definitely does a good job on helping the issue. I think Anna and Miles will be happy with it :)

You can download the source code from here: (a WPF service emulator and the windows phone project)

Download source code


Subscribe

Comments

  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by RaviKumar Bhuvanagiri on Apr 13, 2010 15:02

    Hi...It is nice article to start with Push Notification in WP7 but no need to subscribe for the event HttpNotificationReceived for toast notifications. If we want to catch the toast notification in WP7 application we can subscribe for ShellNotificationReceived event.

    But HttpNotificationReceived event is required for Raw Notifications.

    Thanks

    RK

  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by RaviKumar Bhuvanagiri on Apr 13, 2010 15:05

    Did any one know how to implement the security with WP7 applications?

    Thanks

    RK

  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by zoltan.arvai on Apr 13, 2010 16:32
    RaviKumar: Good observation! Thank you, I missed it :)
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by K on Apr 16, 2010 10:53
    Can the push notification switches on the phone or alert the user via the vibration/sound?
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by zoltan.arvai on Apr 21, 2010 12:50
    I don't know about the vibration since I only have the emulator. It definietly gives you a sound notification as well. If i have to guess i would go with vibration too (IPhone does the same)
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by Amit Kumar on Apr 28, 2010 07:53
    if application is already running then can i still receive push notifications? multiple in sequence while app is still on?
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by zoltan.arvai on Apr 28, 2010 09:17

    Amit Kumar:

    Yes, absolutely! You may want to use raw notifications in that case, since if you app is running it would be nice to automatically present the news in your UI without the phone displaying toasts.

  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by Vivek W on Apr 29, 2010 16:14

    Very good example,

    but could not manage of how to use it what data should i enter in WPF app and for app on Phone

  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by neil on May 01, 2010 18:51
    What is "channel name" in this sample?
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by neil on May 01, 2010 19:10
    Something is broken with the April Refresh CTP. All "channel.ChannelUri" is always NULL.

  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by Chris on May 02, 2010 21:45
    In order to get this sample to work with the April Refresh, you need to add your publisher name to the app element in wmappmanifest. Once you do that, it should work again. http://download.microsoft.com/download/D/9/A/D9A6B6ED-D1CF-4FB3-86BD-62A55959175F/ReleaseNotes.htm
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by Chris on May 11, 2010 17:19
    I'm getting an invalid cross thread exception when the phone is trying to receive the notification.  Anyone else got this?
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by Nilu on May 21, 2010 08:22
    I'm not getting URL,Please help me
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by Chris I on May 24, 2010 20:24

    Need to change the toast message to get it to work for April CTP

    "X-WindowsPhone-Target: toast\r\n\r\n" +
    "<?xml version='1.0' encoding='utf-8'?>" +
    "<wp:Notification xmlns:wp='WPNotification'>" +
    "<wp:Toast>" +
    "<wp:Text1>{0}</wp:Text1>" +
    "</wp:Toast>" +
    "</wp:Notification>";
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by Chris I on May 30, 2010 07:55
    Was anyone able to create a Silverlight app pushing push notifications to the phone?  All I've seen so far are using WPF or a web service.
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7


    posted by Sudhindra Kovalam on Jun 13, 2010 07:36
    Can u detail out the steps on how I can use the WPF sample and try receiving push notifications?
    I am new to this and hence am asking 
  • zoltan.arvai

    RE: Implementing Push Notifications in Windows Phone 7


    posted by zoltan.arvai on Jun 16, 2010 11:05

    Wow so many questions :)

    This sample was created with a previous CTP and it does not work with the current version. Let me update it!

    Just for clarity:

    The WPF application is simulating a service that sends messages to the WP7 through the notification service. So you have to add a link that identifies your phone for the service. The other input is just some bla bla, nothing else. (the message you want to send, like "hello from wpf".

    Now I'm gonna update this article

  • zoltan.arvai

    RE: Implementing Push Notifications in Windows Phone 7


    posted by zoltan.arvai on Jun 16, 2010 12:20
    As soon as you see "Updated" in the title of this article, you'll have access to the changed source code and the updated article as well :) )
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7 - UPDATED (April CTP)


    posted by chris on Jul 18, 2010 13:34
    ok now we need an update for the BETA version haha!
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7 - UPDATED (April CTP)


    posted by zoltan.arvai on Jul 18, 2010 22:28
    Yeah, I guess :) But the api changed to much, so I guess I won't update this article
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7 - UPDATED (April CTP)


    posted by Subash Mandanapu on Oct 21, 2010 01:35
    nice overview. Can you provide more info on service to service protocol.
  • -_-

    RE: Implementing Push Notifications in Windows Phone 7 - UPDATED (April CTP)


    posted by Himanshu on Feb 03, 2011 08:15

    Is there any plan to update this article which works for final release? It will be very helpful to many developers like me.

    Thanks a lot.

  • -_-

    RE: Implementing Push Notifications in Windows Phone 7 - UPDATED (April CTP)


    posted by maor dahan on Feb 24, 2011 17:43
    how we implement in the server side?
  • Steffon

    Re: Implementing Push Notifications in Windows Phone 7 - UPDATED (April CTP)


    posted by Steffon on Mar 21, 2012 21:50
    If you want to make it easier on yourself, it's also possible to use a managed notification service for sending the push notification such as toast and tile to devices running your application. 

    A great benefit of the service, is that you only need to have your application register with their web service, and then your done. They handle all the sending and status changes for your devices. Free subscription with 600 notifications a month, great for developers, and decent plans for larger amounts of notification and attached devices.

  • singaporeproperty5

    Re: Implementing Push Notifications in Windows Phone 7 - UPDATED (April CTP)


    posted by singaporeproperty5 on Apr 10, 2014 04:46
    According to the Iskandar Regional Development Authority (IRDA), the early announcement aims to boost investor confidence and ensure that Medini remains competitive. the premiere

Add Comment

Login to comment:
  *      *