Page MenuHomePhabricator

D6832.id23293.diff
No OneTemporary

D6832.id23293.diff

diff --git a/web/push-notif/service-worker.js b/web/push-notif/service-worker.js
--- a/web/push-notif/service-worker.js
+++ b/web/push-notif/service-worker.js
@@ -1,9 +1,48 @@
// @flow
+import type { WebNotification } from 'lib/types/notif-types.js';
+
+declare class PushMessageData {
+ json(): Object;
+}
+declare class PushEvent extends ExtendableEvent {
+ +data: PushMessageData;
+}
+
+declare var clients: Clients;
+declare function skipWaiting(): Promise<void>;
+
self.addEventListener('install', () => {
- console.log('service worker installed');
+ skipWaiting();
+});
+
+self.addEventListener('activate', (event: ExtendableEvent) => {
+ event.waitUntil(clients.claim());
+});
+
+self.addEventListener('push', (event: PushEvent) => {
+ const data: WebNotification = event.data.json();
+
+ event.waitUntil(
+ (async () => {
+ let body = data.body;
+ if (data.prefix) {
+ body = `${data.prefix} ${body}`;
+ }
+ await self.registration.showNotification(data.title, {
+ body,
+ badge: 'https://web.comm.app/favicon.ico',
+ icon: 'https://web.comm.app/favicon.ico',
+ tag: data.id,
+ data: {
+ unreadCount: data.unreadCount,
+ threadID: data.threadID,
+ },
+ });
+ })(),
+ );
});
-self.addEventListener('activate', () => {
- console.log('service worker activated');
+self.addEventListener('notificationclick', (event: NotificationEvent) => {
+ event.notification.close();
});

File Metadata

Mime Type
text/plain
Expires
Sun, Dec 22, 6:59 PM (18 h, 6 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2690683
Default Alt Text
D6832.id23293.diff (1 KB)

Event Timeline