Node Push Notifications
A node.js module for interfacing with Apple Push Notification, Google Cloud Messaging, Windows Push Notification and Amazon Device Messaging services.

NOTE: Version 1.x has completely been redesigned to be compatible with new apn 2.x and written in es6. Tests have been implemented.
Installation
npm install node-pushnotifications --save
Features
- Powerful and intuitive.
- Multi platform push notifications.
- Automatically detects destination device type.
- Unified error handling.
- Compatible with ES5 through babel transpilation.
Usage
1. Import and setup push module:
const settings = {
gcm: {
id: null,
...
},
apn: {
cert: 'cert.pem',
key: 'key.pem',
...
},
adm: {
client_id: null,
client_secret: null,
...
},
wns: {
client_id: null,
client_secret: null,
notificationMethod: 'sendTileSquareBlock',
...
}
};
const PushNotifications = new require('node-pushnotifications');
const push = new PushNotifications(settings);
iOS: It is recomended to use provider authentication tokens. However, you can also use certificates. See node-apn to see how to prepare cert.pem and key.pem.
### 2. Define destination device ID. You can send to multiple devices, independently of platform, creating an array with different destination device IDs.
const registrationIds = 'INSERT_YOUR_DEVICE_ID';
const registrationIds = [];
registrationIds.push('INSERT_YOUR_DEVICE_ID');
registrationIds.push('INSERT_OTHER_DEVICE_ID');
3. Create a JSON object with a title and message and send the notification.
const data = {
title: 'New push notification',
body: 'Powered by AppFeel',
};
push.send(registrationIds, data, (err, result) => {
if (err) {
console.log(err);
} else {
console.log(result);
}
});
push.send(registrationIds, data)
.then((results) => { ... })
.catch((err) => { ... });
err will be null if all went fine, will return the error otherwise.
Result will contain an array with the following objects:
[
{
method: 'gcm',
multicastId: [],
success: 0,
failure: 0,
message: [{
messageId: '',
regId: value,
error: new Error('unknown'),
}],
},
{
method: 'apn',
...
},
...
]
GCM
NOTE If you provide more than 1.000 registration tokens, they will automatically be splitted in 1.000 chunks (see this issue in gcm repo)
The following parameters are used to create a GCM message. See https://developers.google.com/cloud-messaging/http-server-ref#table5 for more info:
{
collapseKey: data.collapseKey,
priority: data.priority,
contentAvailable: data.contentAvailable || false,
delayWhileIdle: data.delayWhileIdle || false,
timeToLive: data.expiry - Math.floor(Date.now() / 1000) || data.timeToLive || 28 * 86400,
restrictedPackageName: data.restrictedPackageName,
dryRun: data.dryRun || false,
data: data.custom,
notification: {
title: data.title,
body: data.body,
icon: data.icon,
sound: data.sound,
badge: data.badge,
tag: data.tag,
color: data.color,
click_action: data.clickAction || data.category,
body_loc_key: data.locKey,
body_loc_args: data.locArgs,
title_loc_key: data.titleLocKey,
title_loc_args: data.titleLocArgs,
},
}
APN
The following parameters are used to create an APN message:
{
retryLimit: data.retries || -1,
expiry: data.expiry || ((data.timeToLive || 28 * 86400) + Math.floor(Date.now() / 1000)),
priority: data.priority,
encoding: data.encoding,
payload: data.custom,
badge: data.badge,
sound: data.sound || 'ping.aiff',
alert: data.alert || {
title: data.title,
body: data.body,
'title-loc-key': data.titleLocKey,
'title-loc-args': data.titleLocArgs,
'loc-key': data.locKey,
'loc-args': data.locArgs,
'launch-image': data.launchImage,
action: data.action,
},
topic: data.topic || '',
category: data.category || data.clickAction,
contentAvailable: data.contentAvailable,
mdm: data.mdm,
urlArgs: data.urlArgs,
truncateAtWordEnd: data.truncateAtWordEnd,
}
ADM
The following parameters are used to create an ADM message:
const data = Object.assign({}, _data);
const consolidationKey = data.consolidationKey;
const expiry = data.expiry;
const timeToLive = data.timeToLive;
delete data.consolidationKey;
delete data.expiry;
delete data.timeToLive;
const ADMmesssage = {
expiresAfter: expiry - Math.floor(Date.now() / 1000) || timeToLive || 28 * 86400,
consolidationKey,
data,
};
WNS
The following fields are used to create a WNS message:
const notificationMethod = settings.wns.notificationMethod;
const opts = Object.assign({}, settings.wns);
opts.headers = data.headers || opts.headers;
opts.launch = data.launch || opts.launch;
opts.duration = data.duration || opts.duration;
delete opts.notificationMethod;
delete data.headers;
delete data.launch;
delete data.duration;
wns[notificationMethod](regId, data, opts, (err, response) => { ... });
- See wns optional fileds
Note: Please take in mind that if
data.accessToken is supplied then each push notification will be sent after the previous one has been responded. This is due to the fact that in the response while sending a push notification it is possible that Microsoft servers responds with a new accessToken and it should be used for next requests. This can slow down the whole process.
##Resources
Made in Barcelona with <3 and Code