chaching disabled, notification with icon and message now
authorebelcrom <ebelcrom@gmail.com>
Sun, 19 Jan 2020 21:45:11 +0000 (22:45 +0100)
committerebelcrom <ebelcrom@gmail.com>
Sun, 19 Jan 2020 21:45:11 +0000 (22:45 +0100)
public/images/icon.png [new file with mode: 0644]
routes/v1/test/control.js
routes/v1/test/events.js
routes/v1/test/notification.js
routes/v1/test/settings.js
routes/v1/test/status.js

diff --git a/public/images/icon.png b/public/images/icon.png
new file mode 100644 (file)
index 0000000..53b8e91
Binary files /dev/null and b/public/images/icon.png differ
index d546a383495614325c347724a850478b44e2dd6e..ec700d7a97033b20f82c469b7fc35ada162d0842 100644 (file)
@@ -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');
index 95830e93cd44fa5759a4a690ac5887eb419434ac..bd31a75b69c180db5c509e9761c36cf4520ee57e 100644 (file)
@@ -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;
 }
index dc866db536e0f033a1e5b8e107e4cc11e3c3b4f5..c2db2e0318f2546c8749fd8de8a1153b8b89012f 100644 (file)
@@ -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');
index 871fee7f15abf3f514e397358dc02722894333f7..423c1f66077f02d493538d644746e1e785c89f00 100644 (file)
@@ -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));
index 75b025d3cfdb842510b1b5b5e7392112c54f3987..3edfb91ac1bd1b6c83c1cf16d296a96bf31d07c3 100644 (file)
@@ -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');