byteskode-push

byteskode push notifiation with mongoose persistence and kue support
Note: byteskode-push is configured using config using key push
Requirements
Installation
$ npm install --save mongoose byteskode-push
Usage
var mongoose = require('mongoose');
var PushNotification = require('byteskode-push');
var pushNotification = {
to: faker.random.uuid(),
data: {
key1: 'message1',
key2: 'message2'
},
notification: {
title: 'Alert!!!',
body: 'Abnormal data access',
icon: 'ic_launcher'
},
options: {
collapseKey: 'demo',
priority: 'high',
contentAvailable: true,
delayWhileIdle: true,
timeToLive: 3,
restrictedPackageName: 'somePackageName',
dryRun: true
}
};
PushNotification.send(pushNotification, function(error, _pushNotification){
...
});
PushNotification.queue(pushNotification);
API
send(pushNotification:Object,[options:Object], callback(error, pushNotification))
Before send all push notifications are persisted to mongodb using mongoose. pushNotification object should constitute valid node-gcm message and its options.
If running in production
and still want to simulate fake sent
pass an additional object with fake
key set to true
.
To pass node-gcm
send options such as retries
and backoff strategy
also pass them on the options
object.
Example
PushNotification.send(pushNotification, function(error, pushNotification){
...
});
PushNotification.send(pushNotification, {fake:true}, function(error, pushNotification){
...
});
resend([criteria:Object], callback(error, pushNotifications))
Resend will try to resend failed push notifications that are in the database. criteria
is a valid mongoose criteria used to specify which failed push notifications to resend.
Example
PushNotification.resend(fuction(error, pushNotifications){
...
});
PushNotification.resend(criteria, fuction(error, pushNotifications){
...
});
queue(pushNotification:Object,[options:Object], [callback(error, pushNotification)])
Unlike send, queue will save push notification for later processing. After push notification persisted into database mail:queued
event will be fired with an instance of saved push notification. If any error occur an event mail:queue:error
will be fired.
Example
PushNotification.on('push:queued', fuction(pushNotification){
...
...
});
PushNotification.on('push:queue:error', fuction(error){
...
...
});
PushNotification.queue(pushNotification);
requeue([criteria:Object], [callback(error, pushNotification)])
Unlike resend, requeue will fire push:queued
event on every unsent push notification.
Example
PushNotification.requeue();
PushNotification.requeue(criteria);
Configuration Options
Base on your environment setup, ensure you have the following configurations in your config
files.
push: {
apiKey: <your_api_key>,
requestOptions: {},
sendOptions: {},
model: {
name: 'PushNotification',
fields: {}
},
debug: false,
logger: console.log,
kue: {
concurrency: 10,
timeout: 5000,
queue: 'push:queued',
connection: {}
}
}
Kue Integration
To add support to kue
ensure you have installed kue and supply the required configuration. The presence of kue
configuration in push
config options will signal the use of kue
publisher and worker. See Example
$ npm install --save kue
Kue Push Worker
In your worker process start the queued push worker as below
var mongoose = require('mongoose');
var PushNotification = require('byteskode-push');
PushNotification.worker.start();
PushNotifcation.worker.queue.on('job complete', function(id, result) {
console.log('complete', id, result);
});
PushNotifcation.worker.queue.on('error', function(error) {
console.log('error', error);
});
PushNotification.queue(pushNotification);
Testing
$ npm install
$ npm test
Contribute
It will be nice, if you open an issue first so that we can know what is going on, then, fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.
Licence
The MIT License (MIT)
Copyright (c) 2015 byteskode, lykmapipo & Contributors
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.