Skip to content

Commit 2048582

Browse files
committed
Merge pull request #242 from ToothlessGear/v1-simpler-recipient-arg
V1: simpler recipient arg
2 parents 022ce33 + f3d0c9f commit 2048582

3 files changed

Lines changed: 16 additions & 245 deletions

File tree

README.md

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ var gcm = require('node-gcm')('YOUR_API_KEY_HERE');
3838
var message = {
3939
data: { key1: 'msg1' }
4040
};
41-
var regTokens = ['YOUR_REG_TOKEN_HERE'];
41+
var recipient = 'YOUR_REG_TOKEN_HERE';
4242

43-
gcm.send(message, { registrationTokens: regTokens }, function (err, response) {
43+
gcm.send(message, recipient, function (err, response) {
4444
if(err) console.error(err);
4545
else console.log(response);
4646
});
@@ -78,37 +78,28 @@ registrationTokens.push('regToken2');
7878

7979
// Send the message
8080
// ... trying only once
81-
gcm.sendNoRetry(message, { registrationTokens: registrationTokens }, function(err, response) {
81+
gcm.sendNoRetry(message, registrationTokens, function(err, response) {
8282
if(err) console.error(err);
8383
else console.log(response);
8484
});
8585

8686
// ... or retrying
87-
gcm.send(message, { registrationTokens: registrationTokens }, function (err, response) {
87+
gcm.send(message, registrationTokens, function (err, response) {
8888
if(err) console.error(err);
8989
else console.log(response);
9090
});
9191

9292
// ... or retrying a specific number of times (10)
93-
gcm.send(message, { registrationTokens: registrationTokens }, 10, function (err, response) {
93+
gcm.send(message, registrationTokens, 10, function (err, response) {
9494
if(err) console.error(err);
9595
else console.log(response);
9696
});
9797
```
9898

9999
## Recipients
100100

101-
You can send push notifications to various recipient types by providing one of the following recipient keys:
102-
103-
|Key|Type|Description|
104-
|---|---|---|
105-
|to|String|A single [registration token](https://developers.google.com/cloud-messaging/android/client#sample-register), [notification key](https://developers.google.com/cloud-messaging/notifications), or [topic](https://developers.google.com/cloud-messaging/topic-messaging).
106-
|topic|String|A single publish/subscribe topic.
107-
|notificationKey|String|Deprecated. A key that groups multiple registration tokens linked to the same user.
108-
|registrationIds|String[]|Deprecated. Use registrationTokens instead.|
109-
|registrationTokens|String[]|A list of registration tokens. Must contain at least 1 and at most 1000 registration tokens.|
110-
111-
If you provide an incorrect recipient key or object type, an `Error` object will be returned to your callback.
101+
You can send a push notification to various recipient or topic, by providing a notification key, registration token or topic as a string.
102+
Alternatively, you can send it to several recipients at once, by providing an array of registration tokens.
112103

113104
Notice that [you can *at most* send notifications to 1000 registration tokens at a time](https://github.com/ToothlessGear/node-gcm/issues/42).
114105
This is due to [a restriction](http://developer.android.com/training/cloudsync/gcm.html) on the side of the GCM API.
@@ -161,9 +152,9 @@ var gcm = require("node-gcm")('YOUR_API_KEY_HERE', requestOptions);
161152
// Prepare a GCM message...
162153

163154
// Send it to GCM endpoint with modified request options
164-
gcm.send(message, { registrationTokens: regTokens }, function (err, response) {
155+
gcm.send(message, regTokens, function (err, response) {
165156
if(err) console.error(err);
166-
else console.log(response);
157+
else console.log(response);
167158
});
168159
```
169160

lib/sender.js

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -176,30 +176,13 @@ function getRequestBody(message, recipient, callback) {
176176
return nextTick(callback, null, body);
177177
}
178178
if(Array.isArray(recipient)) {
179-
if(!recipient.length) {
180-
return nextTick(callback, 'No recipient provided!');
181-
}
182-
else if(recipient.length == 1) {
183-
body.to = recipient[0];
184-
return nextTick(callback, null, body);
179+
if(recipient.length < 1) {
180+
return nextTick(callback, new Error('Empty recipient array passed!'));
185181
}
186182
body.registration_ids = recipient;
187183
return nextTick(callback, null, body);
188184
}
189-
if (typeof recipient == "object") {
190-
return extractRecipient(recipient, function(err, recipient) {
191-
if(err) {
192-
return callback(err);
193-
}
194-
if (Array.isArray(recipient)) {
195-
body.registration_ids = recipient;
196-
return callback(null, body);
197-
}
198-
body.to = recipient;
199-
return callback(null, body);
200-
});
201-
}
202-
return nextTick(callback, 'Invalid recipient (' + recipient + ', type ' + typeof recipient + ') provided!');
185+
return nextTick(callback, new Error('Invalid recipient (' + recipient + ', type ' + typeof recipient + ') provided (must be array or string)!'));
203186
}
204187

205188
function cleanParams(raw) {
@@ -224,45 +207,4 @@ function nextTick(func) {
224207
}.bind(this));
225208
}
226209

227-
function extractRecipient(recipient, callback) {
228-
var recipientKeys = Object.keys(recipient);
229-
230-
if(recipientKeys.length !== 1) {
231-
return nextTick(callback, new Error("Please specify exactly one recipient key (you specified [" + recipientKeys + "])"));
232-
}
233-
234-
var key = recipientKeys[0];
235-
var value = recipient[key];
236-
237-
if(!value) {
238-
return nextTick(callback, new Error("Falsy value for recipient key '" + key + "'."));
239-
}
240-
241-
var keyValidators = {
242-
to: isString,
243-
topic: isString,
244-
notificationKey: isString,
245-
registrationIds: isArray,
246-
registrationTokens: isArray
247-
};
248-
249-
var validator = keyValidators[key];
250-
if(!validator) {
251-
return nextTick(callback, new Error("Key '" + key + "' is not a valid recipient key."));
252-
}
253-
if(!validator(value)) {
254-
return nextTick(callback, new Error("Recipient key '" + key + "' was provided as an incorrect type."));
255-
}
256-
257-
return nextTick(callback, null, value);
258-
}
259-
260-
function isString(x) {
261-
return typeof x == "string";
262-
}
263-
264-
function isArray(x) {
265-
return Array.isArray(x);
266-
}
267-
268210
module.exports = Sender;

test/unit/senderSpec.js

Lines changed: 4 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -263,67 +263,15 @@ describe('UNIT Sender', function () {
263263
}, 10);
264264
})
265265

266-
it('should set the to field if a single reg token is passed inside the recipient array', function(done) {
266+
it('should set the registration_id field if a single reg token is passed inside the recipient array', function(done) {
267267
var sender = new Sender('myKey');
268268
var m = { data: {} };
269269
var token = "registration token 1";
270270
sender.sendNoRetry(m, [ token ], function () {});
271271
setTimeout(function() {
272272
var body = args.options.json;
273-
expect(body.to).to.deep.equal(token);
274-
expect(body.registration_ids).to.be.an("undefined");
275-
done();
276-
}, 10);
277-
})
278-
279-
it('should set the to field if a single reg token is passed inside the registrationTokens array', function(done) {
280-
var sender = new Sender('myKey');
281-
var m = { data: {} };
282-
var token = "registration token 1";
283-
sender.sendNoRetry(m, { registrationTokens: token }, function () {});
284-
setTimeout(function() {
285-
var body = args.options.json;
286-
expect(body.to).to.deep.equal(token);
287-
expect(body.registration_ids).to.be.an("undefined");
288-
done();
289-
}, 10);
290-
})
291-
292-
it('should set the to field if a single reg token is passed inside the registrationIDs array', function(done) {
293-
var sender = new Sender('myKey');
294-
var m = { data: {} };
295-
var token = "registration token 1";
296-
sender.sendNoRetry(m, { registrationIDs: token }, function () {});
297-
setTimeout(function() {
298-
var body = args.options.json;
299-
expect(body.to).to.deep.equal(token);
300-
expect(body.registration_ids).to.be.an("undefined");
301-
done();
302-
}, 10);
303-
})
304-
305-
it('should set the to field if a topic is passed in', function(done) {
306-
var sender = new Sender('myKey');
307-
var m = { data: {} };
308-
var topic = '/topics/tests';
309-
sender.sendNoRetry(m, { topic: topic }, function () {});
310-
setTimeout(function() {
311-
var body = args.options.json;
312-
expect(body.to).to.deep.equal(topic);
313-
expect(body.registration_ids).to.be.an("undefined");
314-
done();
315-
}, 10);
316-
})
317-
318-
it('should set the to field if a to recipient is passed in', function(done) {
319-
var sender = new Sender('myKey');
320-
var m = { data: {} };
321-
var token = "registration token 1";
322-
sender.sendNoRetry(m, { to: token }, function () {});
323-
setTimeout(function() {
324-
var body = args.options.json;
325-
expect(body.to).to.deep.equal(token);
326-
expect(body.registration_ids).to.be.an("undefined");
273+
expect(body.registration_ids).to.deep.equal([ token ]);
274+
expect(body.to).to.be.an("undefined");
327275
done();
328276
}, 10);
329277
})
@@ -339,120 +287,10 @@ describe('UNIT Sender', function () {
339287
}, 10);
340288
});
341289

342-
it('should pass an error into callback if recipient keys are invalid', function (done) {
343-
var callback = sinon.spy();
344-
var sender = new Sender('myKey');
345-
sender.sendNoRetry({}, {invalid: true}, callback);
346-
setTimeout(function() {
347-
expect(callback.calledOnce).to.be.ok;
348-
expect(callback.args[0][0]).to.be.a('object');
349-
done();
350-
}, 10);
351-
});
352-
353-
it('should pass an error into callback if provided more than one recipient key', function (done) {
354-
var callback = sinon.spy();
355-
var sender = new Sender('myKey');
356-
sender.sendNoRetry({}, {registrationIds: ['string'], topic: 'string'}, callback);
357-
setTimeout(function() {
358-
expect(callback.calledOnce).to.be.ok;
359-
expect(callback.args[0][0]).to.be.a('object');
360-
done();
361-
}, 10);
362-
});
363-
364-
it('should pass an error into callback if registrationIds is not an array', function (done) {
365-
var callback = sinon.spy();
366-
var sender = new Sender('myKey');
367-
sender.sendNoRetry({}, {registrationIds: 'string'}, callback);
368-
setTimeout(function() {
369-
expect(callback.calledOnce).to.be.ok;
370-
expect(callback.args[0][0]).to.be.a('object');
371-
done();
372-
}, 10);
373-
});
374-
375-
it('should pass an error into callback if registrationTokens is not an array', function (done) {
376-
var callback = sinon.spy();
377-
var sender = new Sender('myKey');
378-
sender.sendNoRetry({}, {registrationTokens: 'string'}, callback);
379-
setTimeout(function() {
380-
expect(callback.calledOnce).to.be.ok;
381-
expect(callback.args[0][0]).to.be.a('object');
382-
done();
383-
}, 10);
384-
});
385-
386-
it('should pass an error into callback if to is not a string', function (done) {
387-
var callback = sinon.spy();
388-
var sender = new Sender('myKey');
389-
sender.sendNoRetry({}, {to: ['array']}, callback);
390-
setTimeout(function() {
391-
expect(callback.calledOnce).to.be.ok;
392-
expect(callback.args[0][0]).to.be.a('object');
393-
done();
394-
}, 10);
395-
});
396-
397-
it('should pass an error into callback if topic is not a string', function (done) {
398-
var callback = sinon.spy();
399-
var sender = new Sender('myKey');
400-
sender.sendNoRetry({}, {topic: ['array']}, callback);
401-
setTimeout(function() {
402-
expect(callback.calledOnce).to.be.ok;
403-
expect(callback.args[0][0]).to.be.a('object');
404-
done();
405-
}, 10);
406-
});
407-
408-
it('should pass an error into callback if notificationKey is not a string', function (done) {
409-
var callback = sinon.spy();
410-
var sender = new Sender('myKey');
411-
sender.sendNoRetry({}, {notificationKey: ['array']}, callback);
412-
setTimeout(function() {
413-
expect(callback.calledOnce).to.be.ok;
414-
expect(callback.args[0][0]).to.be.a('object');
415-
done();
416-
}, 10);
417-
});
418-
419-
it('should pass an error into callback if to is empty', function (done) {
420-
var callback = sinon.spy();
421-
var sender = new Sender('myKey');
422-
sender.sendNoRetry({}, {to: ''}, callback);
423-
setTimeout(function() {
424-
expect(callback.calledOnce).to.be.ok;
425-
expect(callback.args[0][0]).to.be.a('object');
426-
done();
427-
}, 10);
428-
});
429-
430-
it('should pass an error into callback if topic is empty', function (done) {
431-
var callback = sinon.spy();
432-
var sender = new Sender('myKey');
433-
sender.sendNoRetry({}, {topic: ''}, callback);
434-
setTimeout(function() {
435-
expect(callback.calledOnce).to.be.ok;
436-
expect(callback.args[0][0]).to.be.a('object');
437-
done();
438-
}, 10);
439-
});
440-
441-
it('should pass an error into callback if notificationKey is empty', function (done) {
442-
var callback = sinon.spy();
443-
var sender = new Sender('myKey');
444-
sender.sendNoRetry({}, {notificationKey: ''}, callback);
445-
setTimeout(function() {
446-
expect(callback.calledOnce).to.be.ok;
447-
expect(callback.args[0][0]).to.be.a('object');
448-
done();
449-
}, 10);
450-
});
451-
452290
it('should pass an error into callback if no recipient provided', function (done) {
453291
var callback = sinon.spy();
454292
var sender = new Sender('myKey');
455-
sender.sendNoRetry({}, {}, callback);
293+
sender.sendNoRetry({}, [], callback);
456294
setTimeout(function() {
457295
expect(callback.calledOnce).to.be.ok;
458296
expect(callback.args[0][0]).to.be.a('object');

0 commit comments

Comments
 (0)