Wednesday, April 16, 2014

GCM (push notification) testing

One of the better efficient ways to communicate messages to your app is via Google's GCM service. This post is about how to test your GCM once you had it set up in your project
(How to implement GCM in your code is very well explained in the official tutorial here).

Our test page will help us easily send GCM messages to a registered device, that way mimicking a server behaviour with very little effort (and without the need to ask the server guy "ok - now try to send a message")

Our test page for sending messages


Installation steps:

  1. If you haven't done it so far - start of with the "getting started" guide. 
  2. In the Google developer console --> API's & Auth --> Credentials --> Create a new key: 
    1. Select "key for browser applications"
    2. Leave the text box emtpy (that will allow all referrers! make sure to use it only in debug)
    3. The final result will look something like:
  3. From the device in which you want to receive the message - extract the `registration ID`. The easiest way is to set a log message once the registration is received, or, via the shared preferences after you had it saved.
  4. Preparing the web page tester - 
    1. Make sure you have a folder that can run PHP scripts
    2. Download the source code for the test page.
    3. Open the 'http://localhost/..../gcm_demo.html" and you're good to go!

Running step:

This is actually the fun part - paste all the data you collected into the web page and hit submit. Witness the message arriving to your device (or witness the error code and fix it :))

Pay attention that GCM as a service doesn't guarantee that the message will arrive, or when it will arrive. Besides building your app structure around this knowledge, you might experience it during your testing.