Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3512219
D6832.id23293.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D6832.id23293.diff
View Options
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
Details
Attached
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)
Attached To
Mode
D6832: [web] Handle notifications in service worker
Attached
Detach File
Event Timeline
Log In to Comment