Skip to content

Instantly share code, notes, and snippets.

@ranacseruet
Last active September 30, 2016 15:16
Show Gist options
  • Save ranacseruet/bfa2630840f2532aff7f to your computer and use it in GitHub Desktop.
Save ranacseruet/bfa2630840f2532aff7f 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'});
*/
@PodWhoo
Copy link

PodWhoo commented Jun 9, 2016

how to get pushNotifier directory ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment