Skip to content

Commit 43ab653

Browse files
Add comms with SW
1 parent 5a8239d commit 43ab653

File tree

1 file changed

+4
-67
lines changed
  • incoming-call-notifications

1 file changed

+4
-67
lines changed

incoming-call-notifications/sw.js

+4-67
Original file line numberDiff line numberDiff line change
@@ -6,81 +6,18 @@ onactivate = () => {
66
return clients.claim();
77
};
88

9-
onmessage = (messageEvent) => {
10-
registration
11-
.showNotification(`ServiceWorkerGlobalScope Title ${messageEvent.data}`, {
12-
body: `ServiceWorkerGlobalScope Body ${messageEvent.data}`,
13-
icon: "../resources/happy.jpg",
14-
})
15-
.then(() => {
16-
messageEvent.source.postMessage(
17-
"ServiceWorkerGlobalScope showNotification() succeeded."
18-
);
19-
})
20-
.catch((error) => {
21-
messageEvent.source.postMessage(
22-
`ServiceWorkerGlobalScope showNotification() failed: ${error}.`
23-
);
24-
});
9+
onfetch = (fetchEvent) => {
10+
console.log(fetchEvent.request.url);
11+
fetchEvent.respondWith(fetch(fetchEvent.request));
2512
};
2613

2714
onnotificationclick = (event) => {
28-
const notification = event.notification;
29-
30-
clients.matchAll().then((resultList) => {
31-
resultList.forEach((client) => {
32-
client.postMessage(
33-
`ServiceWorkerGlobalScope 'click' event for: ${
34-
notification.title
35-
} , timestamp: ${new Date(
36-
notification.timestamp
37-
)}, requireInteraction: ${notification.requireInteraction}, silent: ${
38-
notification.silent
39-
}`
40-
);
41-
notification.close();
42-
});
43-
});
44-
45-
if (event.action === "open_window") {
46-
event.waitUntil(
47-
new Promise((resolve) => {
48-
setTimeout(() => {
49-
clients.openWindow("on-click.html");
50-
resolve();
51-
}, 0);
52-
})
53-
);
54-
} else {
55-
// Focus existing client.
56-
event.waitUntil(
57-
clients.matchAll().then((resultList) => {
58-
if (resultList.length > 0) {
59-
return resultList[0].focus();
60-
}
61-
})
62-
);
63-
}
64-
};
65-
66-
onnotificationclose = (event) => {
6715
clients.matchAll().then((resultList) => {
6816
resultList.forEach((client) => {
6917
const notification = event.notification;
7018
client.postMessage(
71-
`ServiceWorkerGlobalScope 'close' event for: ${
72-
notification.title
73-
} , timestamp: ${new Date(
74-
notification.timestamp
75-
)}, requireInteraction: ${notification.requireInteraction}, silent: ${
76-
notification.silent
77-
}`
19+
`Service-worker: Action '${event.action}' for: ${notification.title}`
7820
);
7921
});
8022
});
8123
};
82-
83-
onfetch = (fetchEvent) => {
84-
console.log(fetchEvent.request.url);
85-
fetchEvent.respondWith(fetch(fetchEvent.request));
86-
};

0 commit comments

Comments
 (0)