summaryrefslogtreecommitdiff
path: root/examples/lib
diff options
context:
space:
mode:
Diffstat (limited to 'examples/lib')
-rw-r--r--examples/lib/okpush/.gitignore3
-rw-r--r--examples/lib/okpush/apn.js57
-rw-r--r--examples/lib/okpush/db.js29
-rw-r--r--examples/lib/okpush/index.js34
-rw-r--r--examples/lib/okpush/package.json14
5 files changed, 137 insertions, 0 deletions
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"
+ }
+}