Last active
September 30, 2016 15:16
-
-
Save ranacseruet/bfa2630840f2532aff7f to your computer and use it in GitHub Desktop.
A simple NodeJS script, to implement apple push notification service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
how to get pushNotifier directory ?