From 82de30525be720083020c53a676f79e5f49c6d55 Mon Sep 17 00:00:00 2001 From: RobinLinus Date: Wed, 13 Mar 2019 00:08:37 +0100 Subject: [PATCH] Add Timeout to automatically close Notifications --- client/scripts/ui.js | 15 +++++++++++---- client/service-worker.js | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/client/scripts/ui.js b/client/scripts/ui.js index f54be6a..af853e2 100644 --- a/client/scripts/ui.js +++ b/client/scripts/ui.js @@ -361,19 +361,26 @@ class Notifications { }); } - _notify(message, body) { + _notify(message, body, closeTimeout = 10000) { const config = { body: body, icon: '/images/logo_transparent_128x128.png', } + let notification; try { - return new Notification(message, config); + notification = new Notification(message, config); } catch (e) { - // android doesn't support "new Notification" if service worker is installed + // Android doesn't support "new Notification" if service worker is installed if (!serviceWorker || !serviceWorker.showNotification) return; - return serviceWorker.showNotification(message, config); + notification = serviceWorker.showNotification(message, config); } + // Notification is persistent on Android. We have to close it manually + if(closeTimeout){ + setTimeout( _ => notification.close(), closeTimeout); + } + + return notification; } _messageNotification(message) { diff --git a/client/service-worker.js b/client/service-worker.js index 4a0dd9e..b90ece0 100644 --- a/client/service-worker.js +++ b/client/service-worker.js @@ -1,4 +1,4 @@ -var CACHE_NAME = 'snapdrop-cache-v1.01'; +var CACHE_NAME = 'snapdrop-cache-v1.02'; var urlsToCache = [ '/', '/styles.css',