summaryrefslogtreecommitdiff
path: root/examples/lib
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2016-10-05 20:37:36 -0400
committerJules Laplace <jules@okfoc.us>2016-10-05 20:37:36 -0400
commitef2cfac2c055cdfa3958374c68a8d0cfe4e4f046 (patch)
treeb8808d63300e7517d0aa0ba4232bb6281e5afd8e /examples/lib
parentae843591da0fea1a57684f26737be0b484718809 (diff)
fixing stuff for node-apn v2
Diffstat (limited to 'examples/lib')
-rw-r--r--examples/lib/okpush/apn.js77
-rw-r--r--examples/lib/okpush/index.js34
-rw-r--r--examples/lib/okpush/package.json4
3 files changed, 61 insertions, 54 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 = {
diff --git a/examples/lib/okpush/index.js b/examples/lib/okpush/index.js
index e8b6549..a39cd35 100644
--- a/examples/lib/okpush/index.js
+++ b/examples/lib/okpush/index.js
@@ -1,8 +1,10 @@
/**
*/
-var OKTemplate = require('../../../app/node_modules/oktemplate')
+var path = require('path')
var passport = require('passport')
+var DigestStrategy = require('passport-http').DigestStrategy;
+var OKTemplate = require('../../../app/node_modules/oktemplate')
var apn = require('./apn')
var db = require('./db')
@@ -23,7 +25,9 @@ function OKPush (options) {
if (!options.config)
throw new Error('Configuration not provided to OKPush')
if (!options.config.notifications)
- throw new Error('Notifications not defined in OKPush')
+ throw new Error('Notifications not provided to OKPush')
+ if (!options.config.bundleId)
+ throw new Error('bundleId not provided to OKPush')
var express = options.express
var router = express.Router()
@@ -31,7 +35,7 @@ function OKPush (options) {
var meta = options.meta
var error = options.errorHandler
// var okcms_db = options.db
-
+
var templateProvider = this._templateProvider = new OKTemplate({
root: path.join(__dirname, './templates'),
debug: meta.debug
@@ -39,14 +43,20 @@ function OKPush (options) {
var templates = {}
templates['index'] = templateProvider.getTemplate('index')
-
- apn.init()
- db.init()
- router.use('/admin/', passport.initialize())
- router.all('/admin/(:path*)?', passport.authenticate('digest', {
- session: false
- }))
+ apn.init(config)
+ db.init(config)
+
+// router.use('/admin/', passport.initialize())
+// router.all('/admin/(:path*)?', passport.authenticate('digest', {
+// session: false
+// }))
+
+ var notifications = {}
+ Object.keys(options.config.notifications).forEach(function(key){
+ var opt = options.config.notifications[key]
+ var note = apn.buildPayload(opt, options.config.bundleId)
+ })
// pass in admin middleware!
router.get('/admin/', function (req, res) {
@@ -71,8 +81,8 @@ function OKPush (options) {
this._router = router
}
-OKExample.prototype.middleware = function () {
+OKPush.prototype.middleware = function () {
return this._router
}
-module.exports = OKExample
+module.exports = OKPush
diff --git a/examples/lib/okpush/package.json b/examples/lib/okpush/package.json
index 4c2f4a1..b5a52c7 100644
--- a/examples/lib/okpush/package.json
+++ b/examples/lib/okpush/package.json
@@ -11,6 +11,8 @@
"dependencies": {
"apn": "^2.1.1",
"lodash": "^4.16.3",
- "mongoose": "^4.6.2"
+ "mongoose": "^4.6.2",
+ "passport": "^0.3.2",
+ "passport-http": "^0.3.0"
}
}