Using MQTT as a GCM replacement for android push notifications


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.


10 thoughts on “Using MQTT as a GCM replacement for android push notifications

  1. How is the battery drain compared to GCM?

    • MQTT is designed to be efficient with battery usage. I’ve had a couple of people mention battery life less than normal, but to be honest that is most likely doing the take which the MQTT message requested, such as taking a photo or playing a sound.

  2. How is the behaviour when the android device is in deep sleep? What about the response time and the reliability in deep sleep mode?

  3. Pingback: GCM XMPP CCS vs MQTT - android

  4. Ollie, thanks for the great post. Can you give any details about your MQTT broker? And, how have you found MQTT overall for push?

  5. While GCM doesn’t have QoS, as of recently it now provides pub/sub. One disadvantage I see with MQTT is that it is creating its own persistent TCP/IP managed by the app, not the O/S. I would guess (I could be wrong) this is not as efficient as using the TCP/IP connection that the O/S uses which in turn GCM uses. If I’m not mistaken MQTT requires each app to have its own persistent TCP/IP connection where as using the O/S push notification the connection is shared?

  6. Reading your blog gave me a lot of interesting informations , it deserves to go viral, you need some initial traffic only.

    How to get initial traffic? Search for: masitsu’s effective method

  7. Hello admin, i must say you have high quality content here.
    Your page can go viral. You need initial traffic boost only.
    How to get it? Search for: Mertiso’s tips go viral

  8. I have noticed you don’t monetize your website, don’t
    waste your traffic, you can earn additional bucks every month because you’ve got high quality content.
    If you want to know how to make extra bucks, search for: Ercannou’s essential adsense alternative

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s