Skip to content

Instantly share code, notes, and snippets.

@kjunine
Created July 24, 2014 01:17
Show Gist options
  • Save kjunine/4469fa896475f77e523f to your computer and use it in GitHub Desktop.
Save kjunine/4469fa896475f77e523f to your computer and use it in GitHub Desktop.
GCM Push with User Notification
'use strict';
var request = require('request'),
Q = require('q'),
config = localrequire.config();
if (process.env.NODE_ENV === 'production') {
var BASE_URI = config.gcm.uri,
PROJECT_ID = config.gcm.project,
API_KEY = config.gcm.key;
var send = function(options) {
var deferred = Q.defer();
request(options, function(err, res, body) {
if (err) return deferred.reject(err);
if (!res) return deferred.reject('response is null');
if (res.statusCode !== 200) return deferred.reject('[CODE: ' + res.statusCode + '] ' + res.body);
try {
var data = JSON.parse(body);
return deferred.resolve(data);
} catch (e) {
return deferred.rejrect(e);
}
});
return deferred.promise;
};
var createOptions = function(api, message) {
var body = JSON.stringify(message);
return {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-length': Buffer.byteLength(body, 'utf8'),
'project_id': PROJECT_ID,
'Authorization': 'key=' + API_KEY
},
uri: BASE_URI + api,
body: body,
timeout: 10000
};
};
var create = function(name, registrations) {
var message = {
'operation': 'create',
'notification_key_name': name,
'registration_ids': registrations
};
var options = createOptions('notification', message);
return send(options);
};
var add = function(name, key, registrations) {
var message = {
'operation': 'add',
'notification_key_name': name,
'notification_key': key,
'registration_ids': registrations
};
var options = createOptions('notification', message);
return send(options);
};
var remove = function(name, key, registrations) {
var message = {
'operation': 'remove',
'notification_key_name': name,
'notification_key': key,
'registration_ids': registrations
};
var options = createOptions('notification', message);
return send(options);
};
var message = function(key, type, data) {
var message = {
'to': key,
'collapse_key': type,
'delay_while_idle': true,
'time_to_live': 60,
'dry_run': false,
'data': {
title: 'StudyGPS',
message: 'You\'ve got a message.',
data: data
}
};
var options = createOptions('send', message);
return send(options);
};
exports.register = function() {
if (arguments.length === 2) {
// name, key
return create(arguments[0], arguments[1])
.then(function(response) {
return response;
});
} else if (arguments.length === 3) {
// name, key, registrations
return add(arguments[0], arguments[1], arguments[2])
.then(function(response) {
return response;
});
}
};
exports.unregister = function(name, key, registrations) {
return remove(name, key, registrations)
.then(function(response) {
return response;
});
};
exports.send = function(key, type, data) {
return message(key, type, data);
};
} else {
exports.register = function() {};
exports.unregister = function() {};
exports.send = function() {};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment