diff options
| -rw-r--r-- | examples/index.js | 32 | ||||
| -rw-r--r-- | examples/lib/okpush/.gitignore | 3 | ||||
| -rw-r--r-- | examples/lib/okpush/apn.js | 57 | ||||
| -rw-r--r-- | examples/lib/okpush/db.js | 29 | ||||
| -rw-r--r-- | examples/lib/okpush/index.js | 34 | ||||
| -rw-r--r-- | examples/lib/okpush/package.json | 14 |
6 files changed, 157 insertions, 12 deletions
diff --git a/examples/index.js b/examples/index.js index ad57a09..357ff60 100644 --- a/examples/index.js +++ b/examples/index.js @@ -50,15 +50,6 @@ var app = okcms.createApp({ image: {type: 'image'}, images: {type: 'triple-captioned-image-list'}, }, -// card: { -// id: {type: 'string', hidden: true}, -// title: {type: 'string'}, -// stack: {type: 'foreign-key', key: 'stack'}, -// }, -// stack: { -// id: {type: 'string', hidden: true}, -// title: {type: 'string'}, -// } }, resources: [ @@ -67,8 +58,6 @@ var app = okcms.createApp({ { type: 'bread' }, { type: 'test' }, { type: 'flour' }, -// { type: 'card' }, -// { type: 'stack' }, ], services: { @@ -95,7 +84,26 @@ var app = okcms.createApp({ dumpfm: { lib: require("./lib/okdumpfm"), - } + }, + + push: { + lib: require("./lib/okpush"), + mongodbUrl: "mongodb://localhost/okpush_example", + apn: { + connection: { + gateway: "gateway.sandbox.push.apple.com", + cert: "/path/to/cert.pem", + key: "/path/to/key.pem" + }, + feedback: { + address: "feedback.sandbox.push.apple.com", + cert: "/path/to/cert.pem", + key: "/path/to/key.pem", + interval: 43200, + batchFeedback: true + } + } + }, }, views: { diff --git a/examples/lib/okpush/.gitignore b/examples/lib/okpush/.gitignore new file mode 100644 index 0000000..e314c5f --- /dev/null +++ b/examples/lib/okpush/.gitignore @@ -0,0 +1,3 @@ + +node_modules + 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 +} diff --git a/examples/lib/okpush/db.js b/examples/lib/okpush/db.js new file mode 100644 index 0000000..5a66b01 --- /dev/null +++ b/examples/lib/okpush/db.js @@ -0,0 +1,29 @@ +// db + +function init (config) { + var db = mongoose.connect(config.mongodbUrl); + mongoose.connection.on('error', errorHandler); + + var pushAssociationSchema = new db.Schema({ + user: { + type: 'String', + required: true + }, + type: { + type: 'String', + required: true, + enum: ['ios', 'android'], + lowercase: true + }, + token: { + type: 'String', + required: true + } + }); + + PushAssociation = db.model('PushAssociation', pushAssociationSchema); +} + +function errorHandler (error) { + console.error('ERROR: ' + error); +};
\ No newline at end of file diff --git a/examples/lib/okpush/index.js b/examples/lib/okpush/index.js new file mode 100644 index 0000000..8b10918 --- /dev/null +++ b/examples/lib/okpush/index.js @@ -0,0 +1,34 @@ +/** + */ + +var apn = require('apn'); + +function OKPush (options) { + if (!(this instanceof OKPush)) return new OKPush(options) + options = options || {} + if (!options.express) + throw new Error('Express not provided to OKPush'); + if (!options.config) + throw new Error('Configuration not provided to OKPush'); + + var express = options.express + var router = express.Router() + var config = options.config + var db = options.db + + router.get('*', function (req, res) { + res.send(config.stuff) + }) + + router.post('*', function (req, res) { + throw new Error('OKPush POST requests not implemented') + }) + + this._router = router +} + +OKExample.prototype.middleware = function () { + return this._router +} + +module.exports = OKExample diff --git a/examples/lib/okpush/package.json b/examples/lib/okpush/package.json new file mode 100644 index 0000000..7af03ae --- /dev/null +++ b/examples/lib/okpush/package.json @@ -0,0 +1,14 @@ +{ + "name": "okpush", + "version": "1.0.0", + "description": "push notification service", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "okfocus <frontdesk@okfoc.us>", + "license": "LNT", + "dependencies": { + "apn": "^2.1.1" + } +} |
