
/**
 * Module dependencies
 */
var Resource      = require('deployd/lib/resource'),
    util          = require('util'),
    gcm           = require('node-gcm');

/**
 * Module setup.
 */
function Gcm( options ) {
  Resource.apply(this, arguments);

  // see https://github.com/ToothlessGear/node-gcm
  // and http://devgirl.org/2013/07/17/tutorial-implement-push-notifications-in-your-phonegap-application/

  // GCM API Server Key
  this.sender = new gcm.Sender(this.config.gcmApiServerKey);
}

util.inherits( Gcm, Resource );

Gcm.prototype.clientGeneration = true;

Gcm.basicDashboard = {
  settings: [
    {
      name        : 'gcmApiServerKey',
      type        : 'string',
      description : 'GCM API Server Key'
    },
    {
      name        : 'defaultTitle',
      type        : 'string',
      description : 'Default title of the push notification. Defaults to \'Notification\''
    },
    {
      name        : 'defaultMessage',
      type        : 'string',
      description : 'Default message of the push notification. Defaults to \'Hi, something came up!\''
    },
    {
      name        : 'defaultTTL',
      type        : 'number',
      description : 'Default time to live of the push notification. Defaults to 3000 seconds'
    }
  ]
};

/**
 * Module methodes
 */
Gcm.prototype.handle = function ( ctx, next ) {

  if ( ctx.req && (ctx.req.method !== 'POST' || !ctx.body.registrationIds || ctx.body.registrationIds.length == 0)) {
    return next();
  }

  var message = new gcm.Message();
  message.timeToLive = this.config.defaultTTL || 3000;
  if (ctx.body.title) {
    message.addData('title', ctx.body.title);
  } else {
    message.addData('title', this.config.defaultTitle || 'Notification' );
  }
  if (ctx.body.message) {
    message.addData('message', ctx.body.message);
  } else {
    message.addData('message', this.config.defaultMessage || 'Hi, something came up!' );
  }
  if (ctx.body.msgcnt) {
    message.addData('msgcnt', ctx.body.msgcnt);
  }
  if (ctx.body.soundname) {
    message.addData('soundname', ctx.body.soundname);
  }
  if (ctx.body.collapseKey) {
    message.collapseKey = ctx.body.collapseKey;
  }
  if (ctx.body.delayWhileIdle) {
    message.delayWhileIdle = ctx.body.delayWhileIdle;
  }
  if (ctx.body.timeToLive) {
    message.timeToLive = ctx.body.timeToLive;
  }

  var registrationIds = ctx.body.registrationIds;
  /**
   * Parameters: message-literal, registrationIds-array, No. of retries, callback-function
   */
  this.sender.send(message, registrationIds, 4, function (err, result) {
    if (err) {
      ctx.done(err);
    } else {
      ctx.done(null, result);
    }
  });
};

/**
 * Module export
 */
module.exports = Gcm;
@@ -487,16 +487,514 @@
 verKey);
+%0A%0A  // the payload data to send...%0A  this.message = new gcm.Message();%0A  this.message.addData('title', this.config.defaultTitle %7C%7C 'Notification' );%0A  this.message.addData('message',this.config.defaultMessage %7C%7C 'Hi, something came up!' );%0A  this.message.timeToLive = this.config.defaultTTL %7C%7C 3000;%0A  // other possible defaults:%0A  //this.message.addData('msgcnt','1');%0A  //this.message.addData('soundname','beep.wav');%0A  //this.message.collapseKey = 'demo';%0A  //this.message.delayWhileIdle = true;
 %0A%7D%0A%0Autil
@@ -1982,144 +1982,113 @@
 var 
-message = new gcm.Message();%0A  message.timeToLive = this.config.defaultTTL %7C%7C 3000;%0A  if (ctx.body.title) %7B%0A    message.addData('title',
+registrationIds = ctx.body.registrationIds;%0A%0A  if (ctx.body.title) %7B%0A    console.log(%22ctx.body.title: %22 +
  ctx
@@ -2099,37 +2099,33 @@
 y.title);%0A  
-%7D else %7B%0A    
+  //this.
 message.addD
@@ -2141,51 +2141,22 @@
 e', 
-this.config.defaultTitle %7C%7C 'Notification' 
+ctx.body.title
 );%0A 
@@ -2192,34 +2192,42 @@
     
-message.addData('
+console.log(%22ctx.body.
 message
-',
+: %22 +
  ctx
@@ -2244,29 +2244,25 @@
 age);%0A  
-%7D else %7B%0A    
+  //this.
 message.
@@ -2284,63 +2284,24 @@
 e', 
-this.config.defaultMessage %7C%7C 'Hi, something came up!' 
+ctx.body.message
 );%0A 
@@ -2324,32 +2324,37 @@
 y.msgcnt) %7B%0A    
+this.
 message.addData(
@@ -2409,32 +2409,37 @@
 oundname) %7B%0A    
+this.
 message.addData(
@@ -2502,32 +2502,37 @@
 lapseKey) %7B%0A    
+this.
 message.collapse
@@ -2592,32 +2592,37 @@
 hileIdle) %7B%0A    
+this.
 message.delayWhi
@@ -2692,16 +2692,21 @@
 ) %7B%0A    
+this.
 message.
@@ -2748,58 +2748,8 @@
  %7D%0A%0A
-  var registrationIds = ctx.body.registrationIds;%0A
   /*
@@ -2866,16 +2866,21 @@
 er.send(
+this.
 message,

debug log output