From b66a9f36504bf2552a8b218ddd5ba91a25e60e6e Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Fri, 29 Apr 2016 11:04:14 +0100 Subject: [PATCH] Apple Push notifications. Trying something else --- app.js | 42 +++++++++++++++++++++++++++++------------- package.json | 10 +++++----- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/app.js b/app.js index 69750ea..52cdb7a 100644 --- a/app.js +++ b/app.js @@ -35,15 +35,37 @@ var isProduction = false; var iosTokens = []; var apnTimer = 0; -var apns = require("apns"), options, connection, notification; +var apn = require('apn'); + +var apnService = new apn.connection({ production: false }); + +apnService.on('connected', function() { + logger.debug('APN Connected'); + }); + +apnService.on('transmitted', function(notification, device) { + logger.debug('APN Notification transmitted to:' + device.token.toString('hex')); + }); + +apnService.on('transmissionError', function(errCode, notification, device) { + logger.error('APNNotification caused error: ' + errCode + ' for device ', device, notification); + if (errCode === 8) { + logger.error('A error code of 8 indicates that the device token is invalid. This could be for a number of reasons - are you using the correct environment? i.e. Production vs. Sandbox'); + } + }); + +apnService.on('timeout', function() { + logger.debug('Connection Timeout'); + }); + +apnService.on('disconnected', function() { + logger.debug('Disconnected from APNS'); + }); + +apnService.on('socketError', logger.error); + -options = { - keyFile : "conf/key.pem", - certFile : "conf/cert.pem", - debug : true -}; -connection = new apns.Connection(options); @@ -255,13 +277,7 @@ app.post('/api/v1/register/ios', function(req, res) { apnConnection.pushNotification(note, myDevice);*/ - logger.warn('Sending Apple Notifications'); - notification = new apns.Notification(); - notification.device = new apns.Device(iosTokens[0]); - notification.alert = "Hello World !"; - - connection.sendNotification(notification); }, 30000); diff --git a/package.json b/package.json index 3f13da1..85fee99 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,10 @@ "method-override": "*", "minibus": "^3.1.0", "morgan": "*", - "mqtt": "^1.7.0", + "mqtt": "^1.10.0", "mqtt_over_websockets": "0.0.1", "node-localstorage": "^1.1.2", - "request": "*", + "request": "^2.72.0", "websocket": "^1.0.22" }, "devDependencies": { @@ -37,7 +37,7 @@ "gulp-autoprefixer": "^3.1.0", "gulp-cache": "^0.4.2", "gulp-concat": "^2.6.0", - "gulp-cssnano": "^2.1.1", + "gulp-cssnano": "^2.1.2", "gulp-debug": "^2.1.2", "gulp-google-webfonts": "0.0.12", "gulp-html-replace": "^1.5.5", @@ -49,14 +49,14 @@ "gulp-notify": "^2.2.0", "gulp-rename": "^1.2.2", "gulp-uglify": "^1.5.3", - "jshint": "^2.9.1", + "jshint": "^2.9.2", "jsonfile": "^2.3.0", "log4js": "^0.6.35", "mocha": "^2.4.5", "mqtt-ws": "^0.2.0", "node-cron": "^1.1.1", "require-dir": "^0.3.0", - "should": "^8.2.2", + "should": "^8.3.1", "string": "^3.3.1", "sugar-date": "^1.5.1", "superagent": "^1.7.2",