Friday, October 29, 2010

Android C2DM: Push It!

Let's imagine an email application for a cell phone; it obviously needs to notify the user whenever a new email has arrived...but how does it actually know when new emails are available?  Well, it could simply ask the server continously if there are any new emails (poll), or we could have the server notify us when any new emails have arrived (push).  But which one should we be using?

As a developer, polling is the easier solution to implement.  We simply have the client wake-up every now and again, and have it call home to the server to check if there's anything new to report...much akin to the child that keeps asking "Are we there yet?".  However, for cell phone applications this approach introduces the issue of polling frequency; poll too frequently, and we're wasting valuable system resources (battery and bandwidth) when for the most part, the answer will be the same.  But poll too infrequently, and we risk the user receiving stale data.  We can skirt around this issue by simply handing the decision to the user via a preference setting...or we could use the push model instead.

When it comes to mobile devices, the push model is, to me at least, a much better solution; it follows the Hollywood Principle of don't call us, we'll call you, meaning that our application only needs to do something when the server actually tells us that new information is available.  This saves those precious system resources, but it still comes at a cost...namely that of increased complexity.  Thankfully, Google has already addressed this with their C2DM service, and being the good fellows that they are, they're opening the service up so that other Android applications can take full advantage of near real-time notifications.

So how does it work?

It works via an existing Google services connection, meaning the user needs to be signed in to a valid Google account on their Android device.  The application can then register itself to receive notifications via this connection, and the C2DM service will respond with a unique key for the application to use.  The application then has to send this key to any third-party server that it wants to receive notifications from, and viola...any time the server needs to notify that particular application on that particular device of something, it simply asks the C2DM service to send out a message.  When the device receives the message, Android knows where to route the message to...even waking up the application if necessary, and as there's no required UI for the messages, the app is free to do whatever it likes.  It may display a message to the user, or it may perform some silent task in the background, like synchronizing data.  How neat is that?

But what of the caveats?
  • The service is only available as of Android 2.2.
  • The service requires the user to be signed in to a valid Google account.
  • Messages can be no more than 1024 bytes in size.
Requiring at least Android 2.2 will eventually become a non-issue as time goes by.  And restricting the message size to 1024 bytes ensures that servers are simply saying "Hey, data is available...come and get it", rather than  trying to send the actual data itself.  But what about requiring a Google account?  Well, that's an extremely small price to pay in my mind...if the user doesn't have (nor want) a Google account, even if it's just for the sake of receiving push notifications, then they'll just have to go to their preference settings and decide for themselves what a good polling frequency should be. :)

No comments:

Post a Comment