hardlounge/client/service-worker.js

56 lines
1.2 KiB
JavaScript
Raw Normal View History

2017-07-10 19:47:03 +00:00
// The Lounge - https://github.com/thelounge/lounge
/* global clients */
"use strict";
self.addEventListener("push", function(event) {
if (!event.data) {
return;
}
const payload = event.data.json();
2017-09-03 15:57:07 +00:00
if (payload.type !== "notification") {
return;
2017-07-10 19:47:03 +00:00
}
2017-09-03 15:57:07 +00:00
// get current notification, close it, and draw new
event.waitUntil(
self.registration
.getNotifications({
tag: `chan-${payload.chanId}`
})
.then((notifications) => {
for (const notification of notifications) {
notification.close();
}
return self.registration.showNotification(payload.title, {
tag: `chan-${payload.chanId}`,
badge: "img/logo-64.png",
icon: "img/touch-icon-192x192.png",
body: payload.body,
timestamp: payload.timestamp,
});
})
);
2017-07-10 19:47:03 +00:00
});
self.addEventListener("notificationclick", function(event) {
event.notification.close();
event.waitUntil(clients.matchAll({
type: "window"
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if ("focus" in client) {
return client.focus();
}
}
if (clients.openWindow) {
return clients.openWindow(".");
}
}));
});