diff options
| author | stone <jules+okfprojz@okfoc.us> | 2017-02-18 19:59:09 -0500 |
|---|---|---|
| committer | stone <jules+okfprojz@okfoc.us> | 2017-02-18 19:59:09 -0500 |
| commit | 794bc930903a836329aff47481d109df10f81069 (patch) | |
| tree | 160499d95dac5d1df01b45490b735d2b60e814e1 /lib/okpush/apn.js | |
| parent | 765527e34208a6e1b83f51eba4a32aa5c239fbc7 (diff) | |
| parent | cf85cc2b75b9c3ead3a693b6fa0feeca5b9e70ba (diff) | |
Merge branch 'cms' of ghghgh.us:stone-island into cms
Diffstat (limited to 'lib/okpush/apn.js')
| -rw-r--r-- | lib/okpush/apn.js | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/okpush/apn.js b/lib/okpush/apn.js new file mode 100644 index 00000000..febd03bb --- /dev/null +++ b/lib/okpush/apn.js @@ -0,0 +1,59 @@ + +var apn = require('apn') +var db = require('./db') +var apnProvider, apnFeedback + +function init (config) { + var apn_config = config.production ? config.apn_production : config.apn_development + config.apn = apn_config + config.apn.connection.key = apn_config.key + config.apn.connection.cert = apn_config.cert + apnProvider = new apn.Provider(config.apn.connection) +} + +function push (note) { + db.getAllDevices(function(err, tokens){ + if (err) { + console.error("Error fetching devices:", err) + return + } + connection.send(note, tokens).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 if (failure.status == 410) { + // `failure.status` is the HTTP status code + // `failure.response` is the JSON payload + // notificationFailed(token, failure.status, failure.response); + db.removeDevice(token) + } + }) + }) + }) +} + +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 = { + init: init, + push: push, + buildPayload: buildPayload +} |
