Update notification counter of all clients on push

This commit is contained in:
Eduard Urbach 2018-03-01 12:08:27 +01:00
parent 0a51b37378
commit 2b62d22f9f
3 changed files with 14 additions and 3 deletions

View File

@ -17,9 +17,6 @@ export async function testNotification(arn: AnimeNotifier) {
await fetch("/api/test/notification", {
credentials: "same-origin"
})
// Update notification counter
arn.notificationManager.update()
}
// Mark notifications as seen

View File

@ -73,6 +73,10 @@ export class ServiceWorkerManager {
let message = JSON.parse(evt.data)
switch(message.type) {
case "new notification":
this.arn.notificationManager.update()
break
case "new content":
if(message.url.includes("/_/")) {
// Content reload

View File

@ -197,6 +197,16 @@ class MyServiceWorker {
onPush(evt: PushEvent) {
var payload = evt.data ? evt.data.json() : {}
// Notify all clients about the new notification so they can update their notification counter
self.clients.matchAll().then(function(clientList) {
for(let client of clientList) {
client.postMessage(JSON.stringify({
type: "new notification"
}))
}
})
// Display the notification
return self.registration.showNotification(payload.title, {
body: payload.message,
icon: payload.icon,