GCM in Groovy

A few weeks ago we had our monthly FedEx Day in the office. I decided to create a Chrome App that receives push notifications from GCM.

I couldn't find a 'middle-ground' solution that didn't involve ripping out the whole kitchen sink, so I decided to create a very thin, simple solution in Groovy. It allows for a simple message POST - that's about it.

package za.co.rrvds

import grails.plugins.rest.client.RestBuilder

class Api4GcmService {

  final String GCM_URL = "https://gcm-http.googleapis.com/gcm/send"
  final String GCM_API_KEY = "<your-gcm-api-key>"

   * POST JSON Payload to GCM
   * @param messageTitle
   * @param messageText
   * @param receiverID
  def postMessage(messageTitle, messageText, receiverID) {

    //Build JSON payload
    def jsonObj = new groovy.json.JsonBuilder()
    jsonObj {
      data {
        title messageTitle
        text messageText
      to receiverID

    //Make REST POST
    def resp = new RestBuilder().post(GCM_URL) {
      header 'Authorization', 'key=' + GCM_API_KEY
      contentType "application/json"
      json jsonObj.toString()