Comparing version 0.11.1 to 0.12.0
@@ -0,0 +0,0 @@ /*! |
@@ -0,0 +0,0 @@ /*! |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ var messageOptions = require("./message-options"); |
@@ -0,0 +0,0 @@ /*! |
@@ -0,0 +0,0 @@ /*! |
@@ -0,0 +0,0 @@ /*! |
var Constants = require('./constants'); | ||
var req = require('request'); | ||
var debug = require('debug')('node-gcm'); | ||
var util = require('util'); | ||
@@ -28,3 +29,22 @@ function Sender(key, options) { | ||
Sender.prototype.sendNoRetry = function(message, registrationIds, callback) { | ||
function extractRecipient(recipient) { | ||
var validKeys = ['registrationIds', 'topic', 'notificationKey']; | ||
var theRecipient; | ||
validKeys.forEach(function(key) { | ||
if (recipient[key]) { | ||
theRecipient = recipient[key]; | ||
return; | ||
} | ||
}); | ||
return { | ||
err: theRecipient ? | ||
null : | ||
'Invalid recipient key(s) ' + Object.keys(recipient) + ' (valid keys: ' + validKeys.join(', ') + ')', | ||
recipient: theRecipient | ||
}; | ||
} | ||
Sender.prototype.sendNoRetry = function(message, recipient, callback) { | ||
if(!callback) { | ||
@@ -36,7 +56,25 @@ callback = function() {}; | ||
if(typeof registrationIds == "string") { | ||
body.to = registrationIds; | ||
if(typeof recipient == "string") { | ||
body.to = recipient; | ||
} | ||
else if (!util.isArray(recipient) && typeof recipient == "object") { | ||
var o = extractRecipient(recipient); | ||
var theRecipient; | ||
if (o.err) { | ||
return callback(o.err); | ||
} | ||
else { | ||
theRecipient = o.recipient; | ||
} | ||
if (util.isArray(theRecipient)) { | ||
body.registration_ids = theRecipient; | ||
} | ||
else { | ||
body.to = theRecipient; | ||
} | ||
} | ||
else { | ||
body.registration_ids = registrationIds; | ||
body.registration_ids = recipient; | ||
} | ||
@@ -82,2 +120,3 @@ | ||
} | ||
if (res.statusCode >= 500) { | ||
@@ -98,5 +137,8 @@ debug('GCM service is unavailable (500)'); | ||
Sender.prototype.send = function(message, registrationIds, options, callback) { | ||
Sender.prototype.send = function(message, recipient, options, callback) { | ||
var backoff, retries; | ||
// In case of failure, will be passed to send() again for retry | ||
var originalRecipient = recipient; | ||
if(typeof options == "object") { | ||
@@ -127,6 +169,19 @@ retries = options.retries; | ||
if(typeof registrationIds == "string") { | ||
registrationIds = [registrationIds]; | ||
if(typeof recipient == "string") { | ||
recipient = [recipient]; | ||
} | ||
if (!registrationIds.length) { | ||
else if (!util.isArray(recipient) && typeof recipient == "object") { | ||
// For topics, passing them to sendNoRetry() as if they were registration IDs | ||
// will put them in the "to" value of the JSON payload, which what we want | ||
var o = extractRecipient(recipient); | ||
if (o.err) { | ||
return callback(o.err); | ||
} | ||
else { | ||
recipient = o.recipient; | ||
} | ||
} | ||
if (util.isArray(recipient) && !recipient.length) { | ||
debug('No RegistrationIds given!'); | ||
@@ -137,3 +192,3 @@ return process.nextTick(callback.bind(this, 'No RegistrationIds given!', null)); | ||
if(retries == 0) { | ||
return this.sendNoRetry(message, registrationIds, callback); | ||
return this.sendNoRetry(message, recipient, callback); | ||
} | ||
@@ -146,7 +201,7 @@ if (backoff > Constants.MAX_BACKOFF_DELAY) { | ||
this.sendNoRetry(message, registrationIds, function(err, result) { | ||
this.sendNoRetry(message, recipient, function(err, result) { | ||
// if we err'd resend them all | ||
if (err) { | ||
return setTimeout(function() { | ||
self.send(message, registrationIds, { | ||
self.send(message, originalRecipient, { | ||
retries: retries - 1, | ||
@@ -160,6 +215,10 @@ backoff: backoff * 2 | ||
var unsentRegIds = [], regIdPositionMap = []; | ||
for (var i = 0; i < registrationIds.length; i++) { | ||
if (result.results[i].error === 'Unavailable') { | ||
regIdPositionMap.push(i); | ||
unsentRegIds.push(registrationIds[i]); | ||
// Responses for messages sent a topic just contain { message_id: '...' } or { error: '...' } | ||
if (result.results) { | ||
for (var i = 0; i < recipient.length; i++) { | ||
if (result.results[i].error === 'Unavailable') { | ||
regIdPositionMap.push(i); | ||
unsentRegIds.push(recipient[i]); | ||
} | ||
} | ||
@@ -166,0 +225,0 @@ } |
{ | ||
"name": "node-gcm", | ||
"description": "a Node.JS wrapper library-port for Google Cloud Messaging for Android", | ||
"version": "0.11.1", | ||
"version": "0.12.0", | ||
"author": "Marcus Farkas <toothlessgear@finitebox.com>", | ||
@@ -34,3 +34,4 @@ "contributors": [ | ||
"Kaija Chang <kaija.chang@gmail.com> (https://github.com/kaija)", | ||
"Mo Kamioner <mkamioner@gmail.com> (https://github.com/mkamioner)" | ||
"Mo Kamioner <mkamioner@gmail.com> (https://github.com/mkamioner)", | ||
"Bastien Léonard <bastien.leonard@gmail.com> (https://github.com/bastienleonard)" | ||
], | ||
@@ -37,0 +38,0 @@ "repository": { |
@@ -35,4 +35,4 @@ # node-gcm | ||
//Now the sender can be used to send messages | ||
sender.send(message, regIds, function (err, result) { | ||
// Now the sender can be used to send messages | ||
sender.send(message, { registrationIds: regIds }, function (err, result) { | ||
if(err) console.error(err); | ||
@@ -42,3 +42,4 @@ else console.log(result); | ||
sender.sendNoRetry(message, regIds, function (err, result) { | ||
// Send to a topic, with no retry this time | ||
sender.sendNoRetry(message, { topic: '/topics/global' }, function (err, result) { | ||
if(err) console.error(err); | ||
@@ -99,3 +100,3 @@ else console.log(result); | ||
// ... trying only once | ||
sender.sendNoRetry(message, registrationIds, function(err, result) { | ||
sender.sendNoRetry(message, { registrationIds: registrationIds }, function(err, result) { | ||
if(err) console.error(err); | ||
@@ -106,3 +107,3 @@ else console.log(result); | ||
// ... or retrying | ||
sender.send(message, registrationIds, function (err, result) { | ||
sender.send(message, { registrationIds: registrationIds }, function (err, result) { | ||
if(err) console.error(err); | ||
@@ -113,3 +114,3 @@ else console.log(result); | ||
// ... or retrying a specific number of times (10) | ||
sender.send(message, registrationIds, 10, function (err, result) { | ||
sender.send(message, { registrationIds: registrationIds }, 10, function (err, result) { | ||
if(err) console.error(err); | ||
@@ -116,0 +117,0 @@ else console.log(result); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23445
420
170