Simple Android C2DM

As I mentioned in my previous post, Learning Android didn’t cover a couple topics, and one of them is Cloud to Device Messaging, or push notifications. When applications are getting data from the internet, there are two ways to keep the data fresh. First is called polling. It means that the app initiates a connection to the server at set intervals to see if there are new data. The problem is determining the polling frequency. Poll too often, you waste a lot of batteries without always getting new data. Poll too rarely, then you risk not getting new data in a timely fashion. The alternative is called pushing, or push notifications (iphone users should be familiar with the term). Instead of the client initiating a connection, the server sends a data packet to the client whenever there is new data, then the client reacts to it. Because there is some overhead to maintain a persistent connection to the server, rolling out a push notification service isn’t a trivial task, but google has done that for us! It’s called Cloud to Device Messaging (C2DM). We piggy back off of the connection that android maintains for gmail, google voice, gchat etc, and register our app to be “waken up” only when the server pings us. Note that the server doesn’t actually send the new data, it only sends an alert that new data is available. This keeps the ping packet small so there is less overhead. It’s up to the client app to receive the notification and then go to the server to retrieve the fresh data. In this post I will explain how to setup this service for your app. I will assume that you went through “Learning Android,” or at least have a basic understanding of the building blocks of Android programming. For the sake of keeping this post under a tolerable reading length, I will only go over the mechanisms. I have checked in a sample c2dm app that’s commented in my github repository, so clone the repository to see the whole picture. I also included a small ruby script to mimic a web server sending c2dm packets to the client device, this way you can debug your implementation without needing to setup a server. By the end of this post you should have an app that you can install on your phone or emulator, and then a way to send it push notifications without requiring any server side coding!

I’ve been following this online book Learning Android which I would highly recommend to anyone that wants to begin learning how to write android apps. It does a great job explaining the concepts of Android development through a non-trivial sample application – a twitter application. It also goes through the process of building the app step by step, improving the app when introducing more advanced concepts. This helps show how to use the building blocks that’s learned to improve an application. It goes through all the building blocks of android apps, and even the more advanced concepts like Android Interface Definition Language (AIDL) and using the Native Development Kit (NDK). I have followed along and built the demo apps while publishing the source code in my github repositories. If you want to learn how to write Android apps, follow along the book and I guarantee that you will not be disappointed!

However, I found that there are some concepts that are missing in this textbook, namely:

  • context menus
  • android testing framework
  • c2dm (cloud to device messaging)

If you do follow along, here are some things to note:

  • The System Services chapter has some bugs in the code posted.
  • The AIDL chapter doesn’t fully explain how to setup the AIDL client app.
  • From a design standpoint there are several design choices that are poorly made for the app in the text. But it does demonstrate how to code the functionality.

Just be sure to read the comments left on the side, as I have left several comments noting the above issues in the text. Enjoy!

Adding excerpts to Webby

So after revisiting my blog I realized that descriptive tutorial entries with code segments are helpful, but they are…long. The front page of my blog was being dragged way too long by those entries. Thus, I needed to add an “excerpt” feature that allows me to partially display an entry and add a ‘read more…’ link for the full post. Rien Heinrichs has done exactly that with his blog created with Webby, and his github page contains the source. I took a look and turns out it was a lot easier than i thought. Here is how to do it:

Added blog tags in Webby

I just found out that Webby is no long under active development =(. What a sad piece of news. However, with the current Webby version, it’s still good enough for me to maintain my blog. Since it’s relatively easy to extend, I decided to add tags to Webby.

I saved a link to Aarom Qian blog post about adding tags to Webby a long time ago, but unfortunately the blog is no longer online. I dug around and was able to find his github repository and the raw textile/erb entry in there. You can see it here: Adding Blog Tags using Webby.

In general his directions were pretty clear and everything worked well. It explained how to setup the tags framework and how to use rake tasks to generate the tag pages. However, I did need to make some minor hacks to get it working. I’ll try to add a tutorial blog entry if i have time. I’d also be more than happy to answer any questions if anyone is having trouble implementing tags.

In the mean time, try to follow along his post, or check out the source of this blog on my github account. Enjoy!

my pic Isaac Liu is currently a Ph.D student at the University of California, Berkeley in the EECS department. He is part of the CHESS (Center for Hybrid and Embedded Software System) group advised by Edward A. Lee. His research interests are in real time systems, parallel architectures and programming models. His current research project is on real time computer architectures (PREcision Timed Machines – PRET).
read more…

office 545K Cory Hall, Berkeley, CA
email liuisaac (at) berkeley (dot) edu
cv html / pdf [ updated: 9/05/11 ]
others facebook linkedin twitter picasa github

Recent Blog Entries

Archive...

Tags