summaryrefslogtreecommitdiff
path: root/examples/lib/okpush/apn.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2016-10-04 19:50:38 -0400
committerJules Laplace <jules@okfoc.us>2016-10-04 19:50:38 -0400
commit95df0e83fdb7f564d64b6c916e674e0db46c116d (patch)
treeb0e68ea99a5a57fdb95aa1046db8fe69d0aefbbb /examples/lib/okpush/apn.js
parent7e525bc4ae191bde7cb40de2c489250cb3e0af0f (diff)
starting push notification module
Diffstat (limited to 'examples/lib/okpush/apn.js')
-rw-r--r--examples/lib/okpush/apn.js57
1 files changed, 57 insertions, 0 deletions
diff --git a/examples/lib/okpush/apn.js b/examples/lib/okpush/apn.js
new file mode 100644
index 0000000..11b8711
--- /dev/null
+++ b/examples/lib/okpush/apn.js
@@ -0,0 +1,57 @@
+
+var config = require('./Config');
+var apn = require('apn');
+var db = require('./db')
+
+function init (config) {
+ var apnConnection = new apn.Connection(config.apn.connection)
+ apnConnection.on('transmissionError', onTransmissionError)
+
+ var apnFeedback = new apn.Feedback(config.apn.feedback)
+ apnFeedback.on('feedback', onFeedback)
+
+ return apnConnection
+}
+
+function onTransmissionError (errorCode, notification, recipient) {
+ console.error('Error while pushing to APN: ' + errorCode)
+
+ if (errorCode === 8 && recipient.token) {
+ var token = recipient.token.toString('hex').toUpperCase()
+
+ console.log('Invalid token: removing device ' + token)
+ pushAssociations.removeDevice(token)
+ }
+}
+
+function onFeedback (deviceInfos) {
+ console.log('Feedback service, number of devices to remove: ' + deviceInfos.length)
+
+ if (deviceInfos.length > 0) {
+ pushAssociations.removeDevices(deviceInfos.map(function (deviceInfo) {
+ return deviceInfo.device.token.toString('hex')
+ })
+ }
+}
+
+var push = function (tokens, payload) {
+ apnSender().pushNotification(payload, tokens)
+}
+
+var buildPayload = function (options) {
+ var notif = new apn.Notification()
+
+ notif.expiry = options.expiry || 0
+ notif.alert = options.alert
+ notif.badge = options.badge
+ notif.payload = options.payload
+ notif.sound = options.sound
+
+ return notif
+}
+
+module.exports = {
+ init: init,
+ push: push,
+ buildPayload: buildPayload
+}