notificator
Version:
Library for handling custom notifications, emails, APS, GCM...
79 lines (59 loc) • 2.39 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var GCMChannel, GCMTemplate, NotificatorChannel, gcm,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
gcm = require('node-gcm');
NotificatorChannel = require('../NotificatorChannel');
GCMTemplate = (function(superClass) {
extend(GCMTemplate, superClass);
function GCMTemplate(payload) {
var key, value;
for (key in payload) {
value = payload[key];
this[key] = value;
}
}
return GCMTemplate;
})(NotificatorChannel.ChannelTemplate);
GCMChannel = (function(superClass) {
extend(GCMChannel, superClass);
function GCMChannel(options) {
this.sender = new gcm.Sender(options.apiKey);
GCMChannel.__super__.constructor.call(this, options);
}
GCMChannel.prototype.sendMessage = function(message, destination, callback) {
message = new gcm.Message(message);
return this.sender.sendNoRetry(message, {
registrationIds: [destination.destination]
}, function(errCode, result) {
if (errCode) {
return callback(new Error('unexpected error with status code: ' + errCode));
}
return callback(null, result);
});
};
GCMChannel.prototype.validateTemplate = function(template) {
return GCMChannel.__super__.validateTemplate.call(this, template);
};
GCMChannel.prototype.validateDestination = function(destination) {
return true;
};
GCMChannel.prototype.transformTemplate = function(template) {
return new GCMTemplate(template.payload || template);
};
GCMChannel.prototype.wrappedDestination = function(destination) {
if (destination != null ? destination.token : void 0) {
destination.destination = destination.token;
delete destination.token;
}
return GCMChannel.__super__.wrappedDestination.call(this, destination);
};
GCMChannel.prototype.name = function() {
return 'GCM';
};
return GCMChannel;
})(NotificatorChannel);
GCMChannel.Template = GCMTemplate;
module.exports = GCMChannel;
}).call(this);