var apn = require('apn') var db = require('./db') var apnProvider, apnFeedback function init (data) { var apn_config = data.production ? data.apn_production : data.apn_development config = {} config.key = apn_config.key config.cert = apn_config.cert config.production = data.production apnProvider = new apn.Provider(config) } function push (channel, note) { db.getAllIOSTokens(channel, function(err, tokens){ if (err) { console.error("Error fetching devices:", err) return } // console.log(note, tokens) apnProvider.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); console.log(failure) // db.removeToken(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 }