From: ebelcrom Date: Sun, 19 Jan 2020 21:45:11 +0000 (+0100) Subject: chaching disabled, notification with icon and message now X-Git-Tag: v1.0.0~8 X-Git-Url: http://www.binomiant.duckdns.org/9wAuyR5S/?a=commitdiff_plain;h=b36442d5aaea264595f8e093a92e59f4dd52a9e0;p=garnod.git chaching disabled, notification with icon and message now --- diff --git a/public/images/icon.png b/public/images/icon.png new file mode 100644 index 0000000..53b8e91 Binary files /dev/null and b/public/images/icon.png differ diff --git a/routes/v1/test/control.js b/routes/v1/test/control.js index d546a38..ec700d7 100644 --- a/routes/v1/test/control.js +++ b/routes/v1/test/control.js @@ -8,8 +8,16 @@ const ev = require('./events'); const events = ev.events; var timer = null; +/* Disables caching in client */ +function nocache(req, res, next) { + res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate'); + res.header('Expires', '-1'); + res.header('Pragma', 'no-cache'); + next(); +} + /* POST /vN/test/control */ -router.post('/', function(req, res, next) { +router.post('/', nocache, function(req, res, next) { // check header if (typeof req.header('X-API-Key-Test') === 'undefined') { log.info('API key not set in request header'); diff --git a/routes/v1/test/events.js b/routes/v1/test/events.js index 95830e9..bd31a75 100644 --- a/routes/v1/test/events.js +++ b/routes/v1/test/events.js @@ -115,7 +115,6 @@ function processTimeout() { log.debug('Timeout, no events'); clearTimeout(timer); timer = null; -//console.log('response:', response); response.status(304).end(''); response = null; } diff --git a/routes/v1/test/notification.js b/routes/v1/test/notification.js index dc866db..c2db2e0 100644 --- a/routes/v1/test/notification.js +++ b/routes/v1/test/notification.js @@ -4,8 +4,16 @@ const qStr = require('query-string'); const router = express.Router(); const dblib = require('./../../../lib/dblib'); +/* Disables caching in client */ +function nocache(req, res, next) { + res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate'); + res.header('Expires', '-1'); + res.header('Pragma', 'no-cache'); + next(); +} + /* POST /vN/test/notification */ -router.post('/', function(req, res, next) { +router.post('/', nocache, function(req, res, next) { // check header if (typeof req.header('X-API-Key-Test') === 'undefined') { log.info('API key not set in request header'); diff --git a/routes/v1/test/settings.js b/routes/v1/test/settings.js index 871fee7..423c1f6 100644 --- a/routes/v1/test/settings.js +++ b/routes/v1/test/settings.js @@ -3,6 +3,7 @@ const log = require('./../../../lib/logger')(__filename.slice(__dirname.length + const qStr = require('query-string'); const router = express.Router(); const dblib = require('./../../../lib/dblib'); +const fs = require('fs'); const webpush = require('web-push'); const delayMin = -1; @@ -201,22 +202,13 @@ function scheduleNotification(delay, notification) { log.debug('Notification set to execute in ' + delay + ' seconds'); } -/* -{ - "endpoint":"https://fcm.googleapis.com/fcm/send/eGS6ZOFQcUk:APA91bGZliEmCLdFcuytpz2KFdLZVNm4YwAdbjaRxyF8tl6vhmuNd7L0NZagys77AjA3GWc3RNhad3i3Bc3rwBQEPKfAX4LZ0jOPzo_heG-WL7MNZx5w9lI2qycrdNk8UiQS0IlhL-j5", - "expirationTime":null, - "keys":{ - "p256dh":"BAMu7XxsCuoB0j1zdaNGXoRterzytIpyG7yxzfEnAaA0SF4xTWJOztnbUoKX4IBdKpteJA9UhhyLI286mZKUlTQ", - "auth":"iPebCb94XG_zeVdfAmKN8Q" - } -} -*/ function executeNotification(notification) { clearTimeout(timer); timer = null; // web push - webpush.setGCMAPIKey('AAAAUtHuYco:APA91bEBTxCRGaez9_glljXAlit3PY5HMwhLSqWYMC1j-jFSp6nvnNqjI42jAVFApQbM0oyAOQjCUilIovB76cwTFxyZTP96wm9n09XwiMRXJjhwiJX1hO32mBB2zwK6X-w7epE1V67K'); + webpush.setGCMAPIKey('AAAAUtHuYco:APA91bEBTxCRGaez9_glljXAlit3PY5HMwhLSqWYMC1j-jFSp6nvnNqj' + + 'I42jAVFApQbM0oyAOQjCUilIovB76cwTFxyZTP96wm9n09XwiMRXJjhwiJX1hO32mBB2zwK6X-w7epE1V67K'); webpush.setVapidDetails( 'mailto:ebelcrom@gmail.com', 'BLDSdGasI5sLks30brbIWvlLMFqzoxxkOs7aW_E9PDBzIO_mDs6-tvtb2U0-BVFDafNd58DJgoXxdK5711FF29c', @@ -231,19 +223,16 @@ function executeNotification(notification) { p256dh: notification.keys.p256dh } }; - const options = { - actions: [ - { - action: 'Open Garage Node', - title: 'Garage Node' - } - ], - body: 'The garage door is open!', - badge: './img/icons/android-chrome-192x192.png', - icon: './img/icons/android-chrome-192x192.png' + + const icon = 'data:image/png;base64,' + + fs.readFileSync(__dirname + '/../../../public/images/icon.png', 'base64'); + const payload = { + message: 'The garage door is open!', + icon: icon }; + log.debug('Paylod length:', JSON.stringify(payload).length); - webpush.sendNotification(pushSubscription, 'The garage door is open!') + webpush.sendNotification(pushSubscription, JSON.stringify(payload)) .then(data => { if (data) { log.info('sent, data:', JSON.stringify(data)); diff --git a/routes/v1/test/status.js b/routes/v1/test/status.js index 75b025d..3edfb91 100644 --- a/routes/v1/test/status.js +++ b/routes/v1/test/status.js @@ -5,8 +5,16 @@ const router = express.Router(); const dblib = require('./../../../lib/dblib'); const fs = require('fs'); +/* Disables caching in client */ +function nocache(req, res, next) { + res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate'); + res.header('Expires', '-1'); + res.header('Pragma', 'no-cache'); + next(); +} + /* GET /vN/status */ -router.get('/', function(req, res, next) { +router.get('/', nocache, function(req, res, next) { // check header if (typeof req.header('X-API-Key-Test') === 'undefined') { log.info('API key not set in request header');