Skip to content

Instantly share code, notes, and snippets.

@victusfate
Forked from ranacseruet/pushNotifier
Created September 30, 2016 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victusfate/c8de50ca7d01006dafd0a738bb0dc050 to your computer and use it in GitHub Desktop.
Save victusfate/c8de50ca7d01006dafd0a738bb0dc050 to your computer and use it in GitHub Desktop.
A simple NodeJS script, to implement apple push notification service
var apn = require("apn")
var apnError = function(err){
console.log("APN Error:", err);
}
var options = {
"cert": "cert.pem",
"key": "key.pem",
"passphrase": null,
"gateway": "gateway.sandbox.push.apple.com",
"port": 2195,
"enhanced": true,
"cacheLength": 5
};
options.errorCallback = apnError;
var feedBackOptions = {
"batchFeedback": true,
"interval": 300
};
var apnConnection, feedback;
module.exports = {
init : function(){
apnConnection = new apn.Connection(options);
feedback = new apn.Feedback(feedBackOptions);
feedback.on("feedback", function(devices) {
devices.forEach(function(item) {
//TODO Do something with item.device and item.time;
});
});
},
send : function (params){
var myDevice, note;
myDevice = new apn.Device(params.token);
note = new apn.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = 1;
note.sound = "ping.aiff";
note.alert = params.message;
note.payload = {'messageFrom': params.from};
if(apnConnection) {
apnConnection.pushNotification(note, myDevice);
}
}
}
/*usage
pushNotifier = require("./pushNotifier");
pushNotifier.init();
//use valid device token to get it working
pushNotifier.process({token:'', message:'Test message', from: 'sender'});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment