Node Push Notifications
A node.js module for interfacing with Apple Push Notification, Google Cloud Messaging, Windows Push Notification, Web-Push Notification and Amazon Device Messaging services.
Installation
npm install rollout-pushnotification --save
Requirements
Node version >= 6.x.x
Features
- Powerful and intuitive.
- Multi platform push notifications.
- Automatically detects destination device type.
- Unified error handling.
- Written in ES6, compatible with ES5 through babel transpilation.
Usage
1. Import and setup push module
Include the settings for each device type. You should only include the settings for the devices that you expect to have. I.e. if your app is only available for android or for ios, you should only include gcm
or apn
respectively.
import PushNotifications from 'rollout-pushnotification';
const settings = {
gcm: {
id: null,
phonegap: false,
...
},
apn: {
token: {
key: './certs/key.p8',
keyId: 'ABCD',
teamId: 'EFGH',
},
production: false
...
},
adm: {
client_id: null,
client_secret: null,
...
},
wns: {
client_id: null,
client_secret: null,
notificationMethod: 'sendTileSquareBlock',
...
},
web: {
vapidDetails: {
subject: '< \'mailto\' Address or URL >',
publicKey: '< URL Safe Base64 Encoded Public Key >',
privateKey: '< URL Safe Base64 Encoded Private Key >',
},
gcmAPIKey: 'gcmkey',
TTL: 2419200,
contentEncoding: 'aes128gcm',
headers: {}
},
isAlwaysUseFCM: false,
};
const push = new PushNotifications(settings);
isAlwaysUseFCM
: use node-gcm to send notifications to GCM (by default), iOS, ADM and WNS.
iOS: It is recommended to use provider authentication tokens. You need the .p8 certificate that you can obtain in your account membership. You should ask for an Apple Push Notification Authentication Key (Sandbox & Production) or Apple Push Notification service SSL (Sandbox & Production). 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');
The PN.send()
method later detects device type and therefore used push method, based on the id stucture. Check out the method PN.getPushMethodByRegId
how this detection works.
Android:
- If you provide more than 1.000 registration tokens, they will automatically be splitted in 1.000 chunks (see this issue in gcm repo)
- You are able to send to device groups or other custom recipients instead of using a list of device tokens (see node-gcm docs). Documentation can be found in the GCM section..
Example:
const data = { ...data, recipients };
3. Send the notification
Create a JSON object with a title and message and send the notification.
const data = {
title: 'New push notification',
topic: 'topic',
body: 'Powered by AppFeel',
custom: {
sender: 'AppFeel',
},
priority: 'high',
collapseKey: '',
contentAvailable: true,
delayWhileIdle: true,
restrictedPackageName: '',
dryRun: false,
icon: '',
image: '',
style: '',
picture: '',
tag: '',
color: '',
clickAction: '',
locKey: '',
titleLocKey: '',
locArgs: undefined,
titleLocArgs: undefined,
retries: 1,
encoding: '',
badge: 2,
sound: 'ping.aiff',
android_channel_id: '',
notificationCount: 0,
alert: {
title: 'title',
body: 'body'
},
silent: false,
launchImage: '',
action: '',
category: '',
urlArgs: '',
truncateAtWordEnd: true,
mutableContent: 0,
threadId: '',
pushType: undefined,
expiry: Math.floor(Date.now() / 1000) + 28 * 86400,
timeToLive: 28 * 86400,
headers: [],
launch: '',
duration: '',
consolidationKey: 'my notification',
};
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, otherwise will return the error from the respective provider module.result
will contain an array with the following objects (one object for each device type found in device registration id's):
[
{
method: 'gcm',
multicastId: [],
success: 0,
failure: 0,
message: [{
messageId: '',
regId: value,
originalRegId: value,
error: new Error('unknown'),
errorMsg: 'some error',
}],
},
{
method: 'apn',
...
},
{
method: 'wns',
...
},
{
method: 'adm',
...
},
{
method: 'webPush',
...
},
]
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:
let custom;
if (typeof data.custom === 'string') {
custom = {
message: data.custom,
};
} else if (typeof data.custom === 'object') {
custom = Object.assign({}, data.custom);
} else {
custom = {
data: data.custom,
};
}
custom.title = custom.title || data.title;
custom.message = custom.message || data.body;
custom.sound = custom.sound || data.sound;
custom.icon = custom.icon || data.icon;
custom.msgcnt = custom.msgcnt || data.badge;
if (opts.phonegap === true && data.contentAvailable) {
custom['content-available'] = 1;
}
const message = new gcm.Message({
collapseKey: data.collapseKey,
priority: data.priority === 'normal' ? data.priority : 'high',
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,
image: data.image,
style: data.style,
picture: data.picture,
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,
android_channel_id: data.android_channel_id,
},
}
data is the parameter in push.send(registrationIds, data)
Note: parameters are duplicated in data and in notification, so in fact they are being send as:
data: {
title: 'title',
message: 'body',
sound: 'mySound.aiff',
icon: undefined,
msgcnt: undefined
sender: 'appfeel-test',
},
notification: {
title: 'title',
body: 'body',
icon: undefined,
image: undefined,
style: undefined,
picture: undefined,
sound: 'mySound.aiff',
badge: undefined,
tag: undefined,
color: undefined,
click_action: undefined,
body_loc_key: undefined,
body_loc_args: undefined,
title_loc_key: undefined,
title_loc_args: undefined,
android_channel_id: undefined
}
In that way, they can be accessed in android in the following two ways:
String title = extras.getString("title");
title = title != null ? title : extras.getString("gcm.notification.title");
Silent push notifications
GCM supports silent push notifications which are not displayed to the user but only used to transmit data.
const silentPushData = {
topic: 'yourTopic',
silent: true,
custom: {
yourKey: 'yourValue',
...
}
}
Internally, silent: true
will tell node-gcm
not to send the notification
property and only send the custom
property. If you don't specify silent: true
then the push notifications will still be visible on the device. Note that this is nearly the same behavior as phoneGap: true
and will set content-available
to true
.
Send to custom recipients (device groups or topics)
In order to override the default behaviour of sending the notifications to a list of device tokens,
you can pass a recipients
field with your desired recipients. Supported fields are to
and condition
as documented in the node-gcm docs.
Example:
const dataWithRecipientTo = { ...yourData, recipients: { to: 'topicName' } };
const dataWithRecipientCondition = { ...yourData, recipients: { condition: 'topicName' } };
push.send(registrationIds, dataWithRecipientTo)
.then((results) => { ... })
.catch((err) => { ... });
Be aware that the presence of a valid data.recipient
field will take precendence over any Android device tokens passed with the registrationIds
.
PhoneGap compatibility mode
In case your app is written with Cordova / Ionic and you are using the PhoneGap PushPlugin,
you can use the phonegap
setting in order to adapt to the recommended behaviour described in
https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#android-behaviour.
const settings = {
gcm: {
id: '<yourId>',
phonegap: true,
},
};
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 === 'normal' ? 5 : 10,
encoding: data.encoding,
payload: data.custom || {},
badge: data.silent === true ? undefined : data.badge,
badge: data.sound === true ? undefined : data.sound,
alert: data.sound === true ? undefined : 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,
collapseId: data.collapseKey,
mutableContent: data.mutableContent || 0,
threadId: data.threadId,
pushType: data.pushType
}
data is the parameter in push.send(registrationIds, data)
Silent push notifications
iOS supports silent push notifications which are not displayed to the user but only used to transmit data.
Silent push notifications must not include sound, badge or alert and have normal priority.
By setting the silent
property to true
the values for sound
, badge
and alert
will be overridden to undefined
.
Priority will be overridden to normal
.
const silentPushData = {
topic: 'yourTopic',
contentAvailable: true,
silent: true,
custom: {
yourKey: 'yourValue',
...
}
}
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) => { ... });
data is the parameter in push.send(registrationIds, data)
Note: Please keep in mind that if data.accessToken
is supplied, each push notification will be sent after the previous one has been responded. This is because Microsoft may send a new accessToken
in the response and it should be used in successive requests. This can slow down the whole process depending on the number of devices to send.
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,
};
data is the parameter in push.send(registrationIds, data)
Web-Push
Data can be passed as a simple string payload. If you do not pass a string, the parameter value will be stringified beforehand.
Settings are directly forwarded to webPush.sendNotification
.
const payload = typeof data === 'string' ? data : JSON.stringify(data);
webPush.sendNotification(regId, payload, settings.web);
A working server example implementation can be found at https://github.com/alex-friedl/webpush-example/blob/master/server/index.js
Resources
LICENSE
The MIT License (MIT)
Copyright (c) 2021 RaindigiIT
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Made in Barcelona n India with <3 and Code