diff options
| author | Jules Laplace <jules@okfoc.us> | 2016-10-05 20:37:36 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2016-10-05 20:37:36 -0400 |
| commit | ef2cfac2c055cdfa3958374c68a8d0cfe4e4f046 (patch) | |
| tree | b8808d63300e7517d0aa0ba4232bb6281e5afd8e /examples/lib/okpush/apn.js | |
| parent | ae843591da0fea1a57684f26737be0b484718809 (diff) | |
fixing stuff for node-apn v2
Diffstat (limited to 'examples/lib/okpush/apn.js')
| -rw-r--r-- | examples/lib/okpush/apn.js | 77 |
1 files changed, 36 insertions, 41 deletions
diff --git a/examples/lib/okpush/apn.js b/examples/lib/okpush/apn.js index 050a466..5e13e15 100644 --- a/examples/lib/okpush/apn.js +++ b/examples/lib/okpush/apn.js @@ -1,54 +1,49 @@ -var config = require('./Config') var apn = require('apn') var db = require('./db') -var apnConnection, apnFeedback +var apnProvider, apnFeedback function init (config) { - apnConnection = new apn.Connection(config.apn.connection) - apnConnection.on('transmissionError', onTransmissionError) - - 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) - db.remove(token) - } + config.apn.connection.key = config.apn.key + config.apn.connection.cert = config.apn.cert + apnProvider = new apn.Provider(config.apn.connection) } -function onFeedback (deviceInfos) { - console.log('Feedback service, number of devices to remove: ' + deviceInfos.length) - - if (deviceInfos.length > 0) { - db.removeDevices(deviceInfos.map(function (deviceInfo) { - return deviceInfo.device.token.toString('hex') +function push (tokens, note) { + tokens.forEach(function(token){ + connection.send(note, token).then( function (response) { + response.sent.forEach( function (token) { + // notificationSent(user, token) + }) + response.failed.forEach( function (failure) { + if (failure.error) { + // A transport-level error occurred (e.g. network problem) + // notificationError(user, token, failure.error); + } else { + // `failure.status` is the HTTP status code + // `failure.response` is the JSON payload + // notificationFailed(token, failure.status, failure.response); + db.removeDevice(token) + } + }) }) - } -} - -function push (tokens, payload) { - apnConnection.pushNotification(payload, tokens) + }) } -function buildPayload (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 +function buildPayload (options, bundleId) { + var note = new apn.Notification() + note.topic = bundleId + if (options.expiry) + note.expiry = Math.floor(Date.now() / 1000) + options.expiry + if (options.alert) + note.alert = options.alert + if (options.badge) + note.badge = options.badge + if (options.payload) + note.payload = options.payload + if (options.sound) + note.sound = options.sound + return note } module.exports = { |
