From 2a3ed8acb211be04915ef8039725a90bdf9583d5 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Fri, 29 Apr 2016 10:52:15 +0100 Subject: [PATCH] Apple Push notifications. Trying something else --- app.js | 47 ++++++++++++--------------------------- cert.pem => conf/cert.pem | 0 key.pem => conf/key.pem | 0 package.json | 2 +- 4 files changed, 15 insertions(+), 34 deletions(-) rename cert.pem => conf/cert.pem (100%) rename key.pem => conf/key.pem (100%) diff --git a/app.js b/app.js index c951fd0..69750ea 100644 --- a/app.js +++ b/app.js @@ -35,33 +35,16 @@ var isProduction = false; var iosTokens = []; var apnTimer = 0; +var apns = require("apns"), options, connection, notification; -var settings = { - gcm: { - id: null, // PUT YOUR GCM SERVER API KEY, - options: {}, - msgcnt: 1, - dataDefaults: { - delayWhileIdle: false, - timeToLive: 4 * 7 * 24 * 3600, // 4 weeks - retries: 4 - } - }, - apn: { - gateway: 'gateway.sandbox.push.apple.com', - badge: 1, - defaultData: { - expiry: 5, // 4 weeks - sound: 'ping.aiff' - } - }, - adm: { - client_id: null, // PUT YOUR ADM CLIENT ID, - client_secret: null, // PUT YOUR ADM CLIENT SECRET, - expiresAfter: 4 * 7 * 24 * 3600, // 4 weeks - } +options = { + keyFile : "conf/key.pem", + certFile : "conf/cert.pem", + debug : true }; -var push = require('node-pushnotifications')(settings); + +connection = new apns.Connection(options); + mqttConnect.setEmitter(busEmitter); @@ -75,10 +58,6 @@ if (process.env.NODE_ENV === 'production') { logger.debug('isProduction:', isProduction); -var options = { }; - -var apnConnection = new apn.Connection(options); - var app = express(); app.set('port', process.env.PORT || 4545); @@ -277,10 +256,12 @@ app.post('/api/v1/register/ios', function(req, res) { apnConnection.pushNotification(note, myDevice);*/ logger.warn('Sending Apple Notifications'); - var data = {title: 'New push notification' , message: 'Powered by Martin', otherfields: 'optionally add more data'}; - push.sendPush(iosTokens, data, function(result) { - logger.debug(result); - }); + + notification = new apns.Notification(); + notification.device = new apns.Device(iosTokens[0]); + notification.alert = "Hello World !"; + + connection.sendNotification(notification); }, 30000); diff --git a/cert.pem b/conf/cert.pem similarity index 100% rename from cert.pem rename to conf/cert.pem diff --git a/key.pem b/conf/key.pem similarity index 100% rename from key.pem rename to conf/key.pem diff --git a/package.json b/package.json index 0e98138..3f13da1 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "devDependencies": { "after": "^0.8.1", "apn": "^1.7.5", + "apns": "^0.1.0", "basic-authentication": "^1.6.2", "chai": "^3.5.0", "cheerio": "^0.20.0", @@ -54,7 +55,6 @@ "mocha": "^2.4.5", "mqtt-ws": "^0.2.0", "node-cron": "^1.1.1", - "node-pushnotifications": "^0.1.4", "require-dir": "^0.3.0", "should": "^8.2.2", "string": "^3.3.1",