librenms/html/service-worker.js
Tony Murray 0b8b97bb68
Push Notifications (Mobile and PC) (#13277)
* Update manifest and add service worker
cleanup icons a bit

* Push notifications WIP

* navigate working

* cleanup

* acknowledge wired up

* Set VAPID keys on composer install

* Component to control notification permissions.

* Allow all user option to validate

* Enable on browser load if transport exists.

* Check for transport before showing user permissions
translations

* Documentation

* style fixes

* access via the attribute model

* fix alerting test

* update schema

* cleanup subscription on disable

* non-configurable db and table for webpush subscriptions (respect system connection)

* revert AlertTransport change
hopefully phpstan can figure it out

* phpstan fixes

* Support custom details display

* Match transport names to brand's preferred display

* less duplicate id errors

* Tests are done in Laravel code now so
remove legacy function usage... could be better, but ok

* Style fixes

* Style fixes 2

* Fix alert test

* Doc updates requires HTTPS and GMP

* unregister subscription when permission is set to denied

* cleanup after user deletion

* delete the right thing

* fix whitespace

* update install docs to include php-gmp

* suggest ext-gmp

* update javascript

* Update functions.php

Co-authored-by: Jellyfrog <Jellyfrog@users.noreply.github.com>
2021-10-06 07:29:47 -05:00

71 lines
2.2 KiB
JavaScript

self.addEventListener('fetch', function (event) {
});
self.addEventListener('push', function (e) {
if (!(self.Notification && self.Notification.permission === 'granted') && localStorage.getItem('notifications') !== 'disabled') {
//notifications aren't supported or permission not granted!
return;
}
if (e.data) {
const msg = e.data.json();
// console.log(msg)
e.waitUntil(self.registration.showNotification(msg.title, {
body: msg.body,
icon: msg.icon,
actions: msg.actions,
data: msg.data
}));
}
});
self.addEventListener('notificationclick', function (event) {
event.notification.close();
if (event.action === 'alert.acknowledge') {
post(`./alert/${event.notification.data.id}/ack`, {state: 1})
} else if (event.action === 'alert.view') {
// navigate to alert
event.waitUntil(self.clients.claim().then(() => self.clients.matchAll({type: 'window'}))
.then(clients => {
return clients.map(client => {
let alert_url = '/alerts?alert_id=' + event.notification.data.id;
// Check to make sure WindowClient.navigate() is supported.
if ('navigate' in client) {
return client.navigate(alert_url);
}
return self.clients.openWindow(alert_url);
});
}));
}
}, false);
let csrf;
function post(url, data, retry = true) {
if (!self.csrf) {
return self.post(url, data, true);
}
return fetch(url, {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-CSRF-Token': self.csrf
},
body: JSON.stringify(data)
}).then((response) => {
if (response.status === 419 && retry === true) {
self.csrf = null; // reset csrf and try again
return self.post(url, data, false);
}
}).catch(e => console.log(e));
}
const fetchCsrf = async () => {
const response = await fetch('/push/token')
self.csrf = await response.text();
}