Sunday 30 June 2013

API for communicating through GCM

At beginning, I started with an API for communicating through GCM with mobile devices running Android. I developed a way of building the messages, sending them and handling the result.
To build the messages, I developed a data type: GCMmessage, where you can customize some parameters for the servers providing the GCM service.
To send the messages you need to establish some server configurations through the data type GCMAppConfig (as the correct Api key provided by Google). Passing this configuration, a GCMmessage and a number of retries as arguments to the function sendGCM, the notifications will be sent to the GCM Servers.
The result is shown in a new data type: GCMresult, which provides some useful information, as selecting the devices that have changed their regIds, or have been unregistered, and so on. The programmer can take his own desitions on how to handle this result in his server.

To show a simple usage of this API for GCM (Network.PushNotify.Gcm), I developed a simple test example of a Yesod server, where devices can register to receive GCM messages and users can send messages through the web service. This is composed of:

+ a Yesod app for registering the devices and sending push notifications.

+ an Android app for registering on GCM and receiving the notifications.

The picture at the bottom shows the main structure of the Google Cloud Message service. We have 3 main points:

The server, which I developed as a Yesod app.
The devices, running Android.
+ The GCM Servers which provide the service.


About the Yesod App:

In this example, I show how to handle with the registration of devices. Also, I provide a web service, so users can send notifications to the devices registered. When the server receives a post request to send a notification to a device, uses the GCM apis for sending the notifications and handling the result in order to correctly actualize the DB. This means removing the devices that have been unregistered, changing the regId of the devices that have changed, and resending the notifications that weren't sent because of an internal error in the GCM Servers.

To try the example:
+ On an Android device, you follow these steps: 
http://gsoc.marcospividori.com.ar/apps
+ If the registration succeded, when you go to: http://gsoc.marcospividori.com.ar/ , you will see your username. And you can start sending notifications through the website.






No comments:

Post a Comment