Using MQTT as a GCM replacement for android push notifications

mqtt

The first thing you might think while reading the title is, why would you not want to use Google Cloud Messaging for android push notifications? Well let me justify it.

I need to support the Kindle Fire and as many other android forks as possible, I’d like to support Android 2.2 and below. Amazon done have push notifications yet, at the time of writing they have a beta push notification service, but I can’t wait for an indeterminate amount of time for that to some out of beta before being able to release an app. I also wanted to support one version of the app and not have different versions for the different app market places and android fork.

To allow push notifications on any android fork I need a transport mechanism that will work across them all. To do this I researched quite a few alternatives. I happened to come across MQTT and discovered that Facebook use an MQTT background service to receive messages to and from the Facebook and Facebook Messenger apps.

What benefits does MQTT have over GCM?

  • Pub/Sub: A client can subscribe to multiple topics
  • Quality of Service (QoS): GCM is pretty much “fire and forget”. You receive acknowledgement that the message has been received but the server, but not that it is delivered to the client, so you will never know if the client got the message unless they communicate back to the server (which my code used to do). MQTT has 3 levels of QoS. Which vary from “fire and forget” to full acknowledgement that the client received the message.

But there are a few things you might miss if you move away from GCM:

  • The massive Google infrastructure
  • You don’t need to manage your own long-lived TCP connections in a service

For my app I ended up doing was using a similar technique to Facebook. I created a new Service has a permanent connection open to an MQTT broker on a server I host with Linode (which I highly recommend). It receives a message on a topic specific to the device that is connected to it, then it broadcasts a message in a similar way to GCM does. This meant that the rest of my code needed minimal changes in order to support this new transport. The code was based on an awesome blog post from Dale Lane. I recommend giving that a good read through, it goes into a lot of detail and a full implementation of the service.

If you are interested, the app I am developing that uses this technique is Lockate, android mobile security software.

The blog is back

For several years now I’ve been redirecting my ollieparsley.com domain to my about.me page. I’ve been working on several small projects and wanted a place to talk about them and blog about things I care about, the easiest way was to sort my site out again. This was part due to the fact that about.me haven’t implemented the most requested feature of supporting CNAME’s.

Anyway enough of a mini rant, enjoy my blog! :p