/AndroidGCMTutorial

In this project I go through all the step necessary to create 1) a python web app which a REST API that communicates through GCM to 2) an Android client application which synchronizes with the server through REST and GCM. Everything authenticated with access_token generated on the device.

Primary LanguagePython

Goals

  1. Learn how to implement an Android app which utilises GCM, to a server-side app.
  2. Learn how to make a server-side app with a REST interface and connections to GCM.
  3. Be able to deploy the server to Google App Engine.

This project is divided into two (three) parts: the Android client and the server app (and a version of the server suitable for App Engine). My own motivations for this project is to learn how to make a server side app. I already know how to make Android apps but know very little about web programming. So this will be outside of my comfort zone. I have used REST interfaces plenty, but never implemented one before. Likewise, I am familiar with GCM but have only seen crappy sample implementations so far.

I stipulate the following requirements for the projects:

Serverside:

  • Should be written in Python
  • Have a REST api
  • Use GCM
  • Require login but don't handle passwords

Androidside:

  • No passwords
  • All network stuff should be done in a SyncAdapter
  • Get push updates through GCM
  • Full syncs using REST

Can't I read all this somewhere already?

Yes you can! There are a lot of documents, tutorials, sample code and help you can get online, many times straight from Google themselves! The problem is that there is a lot of documents, tutorials etc. And they all focus on one thing. Either it's a document about making a SyncAdapter, or it's a tutorial on GCM, or a document about making a WebApp.

It's all about context. Maybe I haven't looked hard enough, but to my eyes no one gives you the big picture and the technical details. Google's own documents on GCM is a prime example: they talk at length about the structure of the system, and give you plenty of sample code for the client side, but are incredibly vague on the server bit. They mention the requirement of an application server but give no help in creating one.

Again, it's all about context. The GCM docs focus on how to implement the GCM bits, so it's not that weird that it doesn't go into newbie hints on application servers. But no one else seems to do that either. Another thing is that there are a ton of documents that are out-dated.

In my opinion it is best to stay away from libraries developed by Google, whenever possible. They build good frameworks but the libraries are just not user friendly.

In short, I will attempt to deliver both the big picture and the nitty gritty details in this project that no one else seems keen on writing down.

Target audience

This is not a tutorial for someone looking to write their first Android app or program. I assume that you have worked with Android before. I also expect that you are familiar with some concepts, at least in theory, like REST APIs or Sqlite. I do try to explain the idea behind the implementations but I will not go into detail as to why I had that particular idea over another.

Intent

I do not claim that this code is ready for anything other than a base to build upon for your own projects. It is NOT PRODUCTION READY. People with more expertise than myself can probably improve upon several aspects, and I welcome any suggestions/merge requests that can improve the project. As I said, when it comes to server apps, I'm also something of a newbie.

Why Python for the server app?

Python is by far my current favorite language. It takes so much less bloat compared to Java to translate an idea into a working implementation. Hopefully, I will convince you along the way that Python is awesome. Even if you are not familiar with Python, I bet that it will be possible for you to understand the program completely anyway. Because it's that simple.

Format of the tutorial

The READMEs go through the construction of the apps step by step. They specify what SHA1 corresponds to that state in the tutorial. That means that you can examine all pieces of the project at that particular state by doing:

git checkout <sha1>

Just do this to get back to the normal state

git checkout master

What is particularly useful is that you can do direct comparisons to see all changes between one state and the next by doing:

git diff <sha1> <sha1>

And you can show the differences for a particular file/folder with:

git diff <sha1> <sha1> <filename-or-folder>

I highly recommend doing it to see exactly what lines of code were added to implement some feature. Here's an example that would show what was added to enable authentication with Google in the server-app:

git diff 92e4ed4383b4a6 e4c340b30155 server-app/app.py server-app/google_auth.py

Requirements

You should read the official docs on GCM

You also need to create a project in the Google API console

For the purpose of this tutorial, the docs are somewhat confusing in regards to what key you need to generate. You need to generate the following types of keys:

  • Client ID for installed applications (Guide)
  • Simple API Access (Guide)

The Simple API key will be used by the server to verify the validity of authentication tokens generated by the client. To generate the token, the clients need the Client ID for installed apps, but you won't need a specific key in that case since Google will verify the package name of the application. You will need the project number in the client though, which you can see in the address bar of your browser:

https://code.google.com/apis/console/#project:8646666293:access

Where 8646666293 is your project number.

Android app

In this folder I implement an android app. This will be fairly straightforward. Have a look in android-client for that.

Server app

In this folder I construct an app we can deploy on a webserver somewhere. Go into the server-app folder and see how that's done.

The server is coded in Python because Java is mostly bloat.

Google App Engine

I have also made a version of the server which can be deployed to App Engine

A few tweaks were necessary for it to function, but structurally it is almost the same as the regular server.

Result

You can download the finished app and try it out for yourself here:

Releases

There are two versions to choose from. One connects to a running version of the server on a computer controlled by me. The other connects to a version of the app engine server, running on app engine.

It looks like this:

As a side note, you can experiment with the server running on App Engine by using the API Explorer.

If you have questions or feedback

You can contact me by e-mail. If you find some part of the tutorial hard to understand or want something clarified, you can also create an issue. The same goes if you find an error. As I said, I am sort of unfamiliar with App Engine and server side stuff at the time I'm writing this, so there might very well be mistakes. Do the Internet a favor and report them if you find any.