diff options
Diffstat (limited to 'ui/service-worker.js')
| -rw-r--r-- | ui/service-worker.js | 44 |
1 files changed, 24 insertions, 20 deletions
diff --git a/ui/service-worker.js b/ui/service-worker.js index 391705c..81d0752 100644 --- a/ui/service-worker.js +++ b/ui/service-worker.js @@ -26,26 +26,30 @@ function countUnreadChannels() { } self.addEventListener('push', (event) => { - // Let's show a notification right away so Safari doesn't tell Apple to be - // mad at us: - event.waitUntil( - self.registration.showNotification('Test notification', { - actions: [], - body: event.data.text(), - }), - ); + const data = event.data.json(); + if (data.type === 'heartbeat') { + // Let's show a notification right away so Safari doesn't tell Apple to be + // mad at us: + event.waitUntil( + self.registration.showNotification('Test notification', { + actions: [], + body: event.data.text(), + }), + ); + } - const data = event.data?.json(); - // Now we can do slower things that might fail: - conversationReadStatus[data.conversation] ||= { lastRead: null, lastMessage: null }; - conversationReadStatus[data.conversation].lastMessage = new Date(); - event.waitUntil( - (async () => { - if (navigator.setAppBadge) { - navigator.setAppBadge(countUnreadChannels()); - } - })(), - ); + if (data.type === 'message' && data.event === 'sent') { + // Now we can do slower things that might fail: + conversationReadStatus[data.conversation] ||= { lastRead: null, lastMessage: null }; + conversationReadStatus[data.conversation].lastMessage = new Date(); + event.waitUntil( + (async () => { + if (navigator.setAppBadge) { + await navigator.setAppBadge(countUnreadChannels()); + } + })(), + ); + } }); // The client has to tell us when it has read a conversation: @@ -62,7 +66,7 @@ self.addEventListener('message', (event) => { event.waitUntil( (async () => { if (navigator.setAppBadge) { - navigator.setAppBadge(countUnreadChannels()); + await navigator.setAppBadge(countUnreadChannels()); } })(), ); |
