node-sender
NodeJS Library to push on iPhone, Android, WindowsPhone.
Presentation
node-sender can be easily and simply send push notifications on major
existing mobile systems.
License
Install
npm install node-sender
Quick Examples
At the beginning
You need to import the sender library.
var Sender = require('node-sender');
Android (GCM)
var sender = Sender.send({
log : log,
type : Sender.constants.TYPE_ANDROID,
message : {
title : "your title",
body : "Test android push",
any_other_Key : "that you want to send"
},
tokens : ["your token"],
config : {
apiKey : "GCM Api-KEY",
ttl : 7200
}
});
iOS (APNs HTTP/2)
var Sender = require('node-sender');
var sender = Sender.send({
log : log,
type : Sender.constants.TYPE_IOS,
message : {
alert : "your notification",
badge : 1,
sound : "cat.caf"
},
tokens : ["your token"],
config : {
cert : "path to your cert file",
key : "path to your key file",
ttl : 7200,
production : true
}
});
Windows Phone (WNS)
var sender = Sender.send({
log : log,
type : Sender.constants.TYPE_WP,
message : {
msge : "Message "
},
tokens : {
url : ["tokenUrl,...."]
},
config : {
sid : "your sid",
secret : "your secret",
ttl : 7200
}
});
⚠️ Important: If there is a parameters problem, it'll be throw and not send through callback or event system !
How to retrieve the failed/successful notifications
❗️ Important: You can't use the both system at the same time !
Callback
You can use the callback system like that
(the token and config SID/Secret are wrong ;) )
Sender.send({
type : Sender.constants.TYPE_WP,
message : {
msge : "My beautiful notification"
},
tokens : {
url : ["AOBCIAHJSJAOPFIABFNHAONODBF"]
},
config : {
sid : "ogdjqfqopfnsdopbgfdoqfn",
secret : "fdognpsdfogopdfgjonfdgodfgn",
}
}, (error, result)=> {
if (!error) {
console.log(result.successful);
console.log(result.failed);
console.log(result.unregistered);
}
});
EventEmitter
Or you can use the EventEmitter system
var sender = Sender.send({
type : Sender.constants.TYPE_WP,
message : {
msge : "My beautiful notification"
},
tokens : {
url : ["AOBCIAHJSJAOPFIABFNHAONODBF"]
},
config : {
sid : "ogdjqfqopfnsdopbgfdoqfn",
secret : "fdognpsdfogopdfgjonfdgodfgn",
}
});
sender.on("error", (err) => {
});
sender.on("successful", (token) => {
});
sender.on("failed", (error) => {
});
sender.on("unregistered", (token) => {
});
sender.on("end", (results) => {
console.log(results.successful);
console.log(results.failed);
console.log(results.unregistered);
});
TODO