Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V1: remove Message #239

Merged
merged 12 commits into from
May 30, 2016
85 changes: 28 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@ If you are new to GCM you should probably look into the [documentation](https://
According to below **Usage** reference, we could create such application:

```js
var gcm = require('node-gcm');
var gcm = require('node-gcm')('YOUR_API_KEY_HERE');

var message = new gcm.Message({
var message = {
data: { key1: 'msg1' }
});

// Set up the sender with you API key, prepare your recipients' registration tokens.
var sender = new gcm.Sender('YOUR_API_KEY_HERE');
};
var regTokens = ['YOUR_REG_TOKEN_HERE'];

sender.send(message, { registrationTokens: regTokens }, function (err, response) {
gcm.send(message, { registrationTokens: regTokens }, function (err, response) {
if(err) console.error(err);
else console.log(response);
});
Expand All @@ -52,21 +49,17 @@ sender.send(message, { registrationTokens: regTokens }, function (err, response)
## Usage

```js
var gcm = require('node-gcm');
var gcm = require('node-gcm')('insert Google Server API Key here');

// Create a message
// ... with default values
var message = new gcm.Message();

// ... or some given values
var message = new gcm.Message({
collapseKey: 'demo',
// Create a message (all possible values shown)
var message = {
collapse_key: 'demo',
priority: 'high',
contentAvailable: true,
delayWhileIdle: true,
timeToLive: 3,
restrictedPackageName: "somePackageName",
dryRun: true,
content_available: true,
delay_while_idle: true,
time_to_live: 3,
restricted_package_name: "somePackageName",
dry_run: true,
data: {
key1: 'message1',
key2: 'message2'
Expand All @@ -76,21 +69,7 @@ var message = new gcm.Message({
icon: "ic_launcher",
body: "This is a notification that will be displayed ASAP."
}
});

// Change the message data
// ... as key-value
message.addData('key1','message1');
message.addData('key2','message2');

// ... or as a data object (overwrites previous data object)
message.addData({
key1: 'message1',
key2: 'message2'
});

// Set up the sender with you API key
var sender = new gcm.Sender('insert Google Server API Key here');
};

// Add the registration tokens of the devices you want to send to
var registrationTokens = [];
Expand All @@ -99,28 +78,28 @@ registrationTokens.push('regToken2');

// Send the message
// ... trying only once
sender.sendNoRetry(message, { registrationTokens: registrationTokens }, function(err, response) {
gcm.sendNoRetry(message, { registrationTokens: registrationTokens }, function(err, response) {
if(err) console.error(err);
else console.log(response);
});

// ... or retrying
sender.send(message, { registrationTokens: registrationTokens }, function (err, response) {
gcm.send(message, { registrationTokens: registrationTokens }, function (err, response) {
if(err) console.error(err);
else console.log(response);
});

// ... or retrying a specific number of times (10)
sender.send(message, { registrationTokens: registrationTokens }, 10, function (err, response) {
gcm.send(message, { registrationTokens: registrationTokens }, 10, function (err, response) {
if(err) console.error(err);
else console.log(response);
});
```

## Recipients

You can send push notifications to various recipient types by providing one of the following recipient keys:


|Key|Type|Description|
|---|---|---|
|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).
Expand All @@ -137,21 +116,13 @@ This is due to [a restriction](http://developer.android.com/training/cloudsync/g
## Notification usage

```js

var message = new gcm.Message();

// Add notification payload as key value
message.addNotification('title', 'Alert!!!');
message.addNotification('body', 'Abnormal data access');
message.addNotification('icon', 'ic_launcher');

// as object
message.addNotification({
title: 'Alert!!!',
body: 'Abnormal data access',
icon: 'ic_launcher'
});

var message = {
notification: {
title: 'Alert!!!',
body: 'Abnormal data access',
icon: 'ic_launcher'
}
};
```

### Notification payload option table
Expand Down Expand Up @@ -184,13 +155,13 @@ var requestOptions = {
timeout: 5000
};

// Set up the sender with your API key and request options
var sender = new gcm.Sender('YOUR_API_KEY_HERE', requestOptions);
// Set up gcm with your API key and request options
var gcm = require("node-gcm")('YOUR_API_KEY_HERE', requestOptions);

// Prepare a GCM message...

// Send it to GCM endpoint with modified request options
sender.send(message, { registrationTokens: regTokens }, function (err, response) {
gcm.send(message, { registrationTokens: regTokens }, function (err, response) {
if(err) console.error(err);
else console.log(response);
});
Expand Down
24 changes: 13 additions & 11 deletions examples/notification.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
var gcm = require('../lib/node-gcm');

var message = new gcm.Message();

message.addData('hello', 'world');
message.addNotification('title', 'Hello');
message.addNotification('icon', 'ic_launcher');
message.addNotification('body', 'World');
//Replace your developer API key with GCM enabled here
var gcm = require('../index')('AIza*******************5O6FM');

var message = {
data: {
hello: 'world'
},
notification: {
title: 'Hello',
icon: 'ic_launcher',
body: 'World'
}
};

//Add your mobile device registration tokens here
var regTokens = ['ecG3ps_bNBk:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxXl7TDJkW'];
//Replace your developer API key with GCM enabled here
var sender = new gcm.Sender('AIza*******************5O6FM');

sender.send(message, regTokens, function (err, response) {
gcm.send(message, regTokens, function (err, response) {
if(err) {
console.error(err);
} else {
Expand Down
28 changes: 8 additions & 20 deletions lib/message-options.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,31 @@
/**
* This module defines all the arguments that may be passed to a message.
*
* Each argument may contain a field `__argName`, if the name of the field
* should be different when sent to the server.
*
* The argument may also contain a field `__argType`, if the given
*
* Each argument may contain a field `__argType`, in which case the given
* argument must be of that type. The types are the strings resulting from
* calling `typeof <arg>` where `<arg>` is the argument.
*
* Other than that, the arguments are expected to follow the indicated
* structure.
*/

module.exports = {
collapseKey: {
__argName: "collapse_key",
collapse_key: {
__argType: "string"
},
priority: {
__argType: "string"
},
contentAvailable: {
__argName: "content_available",
content_available: {
__argType: "boolean"
},
delayWhileIdle: {
__argName: "delay_while_idle",
delay_while_idle: {
__argType: "boolean"
},
timeToLive: {
__argName: "time_to_live",
time_to_live: {
__argType: "number"
},
restrictedPackageName: {
__argName: "restricted_package_name",
restricted_package_name: {
__argType: "string"
},
dryRun: {
__argName: "dry_run",
dry_run: {
__argType: "boolean"
},
data: {
Expand Down
66 changes: 0 additions & 66 deletions lib/message.js

This file was deleted.

4 changes: 1 addition & 3 deletions lib/node-gcm.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
exports.Constants = require('./constants');
exports.Message = require('./message');
exports.Sender = require('./sender');
module.exports = require("./sender");
18 changes: 17 additions & 1 deletion lib/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var Constants = require('./constants');
var _ = require('lodash');
var request = require('request');
var debug = require('debug')('node-gcm');
var messageOptions = require("./message-options");

function Sender(key, options) {
if (!(this instanceof Sender)) {
Expand Down Expand Up @@ -168,7 +169,7 @@ Sender.prototype.sendNoRetry = function(message, recipient, callback) {
};

function getRequestBody(message, recipient, callback) {
var body = message.toJson();
var body = cleanParams(message);

if(typeof recipient == "string") {
body.to = recipient;
Expand Down Expand Up @@ -201,6 +202,21 @@ function getRequestBody(message, recipient, callback) {
return nextTick(callback, 'Invalid recipient (' + recipient + ', type ' + typeof recipient + ') provided!');
}

function cleanParams(raw) {
var params = {};
Object.keys(raw).forEach(function(param) {
var paramOptions = messageOptions[param];
if(!paramOptions) {
return console.warn("node-gcm ignored unknown message parameter " + param);
}
if(paramOptions.__argType != typeof raw[param]) {
return console.warn("node-gcm ignored wrongly typed message parameter " + param + " (was " + typeof raw[param] + ", expected " + paramOptions.__argType + ")");
}
params[param] = raw[param];
});
return params;
}

function nextTick(func) {
var args = Array.prototype.slice.call(arguments, 1);
process.nextTick(function() {
Expand Down
Loading