-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsw.js
More file actions
104 lines (95 loc) · 3.34 KB
/
sw.js
File metadata and controls
104 lines (95 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function broadcastMessageToClients(clients, body) {
clients.forEach((client) => {
client.postMessage({
visible: client.visibilityState === "visible",
data: body,
});
});
}
function showNotification(body) {
const title = "New incoming call";
const icon = "../common/style/favicon-180x180.png";
const options = {
body: title,
icon,
data: {
body,
},
};
return this.registration.showNotification(title, options);
}
function postMessageToClients(message) {
return clients
.matchAll({ type: "window", includeUncontrolled: true })
.then((clients) => {
broadcastMessageToClients(clients, message);
});
}
function delay(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
this.addEventListener("push", (event) => {
console.log("ServiceWorker Push: ", event);
const body = event.data.json();
event.waitUntil(
clients
.matchAll({ includeUncontrolled: true, type: "window" })
.then((clients) => {
if (clients.length > 0) {
broadcastMessageToClients(clients, body);
let anyClientFocused = false;
clients.forEach((client) => {
if (client.visibilityState === "visible") {
anyClientFocused = true;
}
});
if (!anyClientFocused) {
return showNotification(body);
}
return Promise.resolve();
}
return showNotification(body);
}),
);
});
/**
* IMPORTANT: Please note that this event is not supported on Safari on iOS.
* See: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/notificationclick_event#browser_compatibility
*
* As a result, the reference app will not handle push notifications correctly when the application is terminated on iOS.
* (It will work correctly only when the app is in the background or foreground).
* The reason for this is that, when terminated, iOS always routes to the "start URL" on notification click event (the login screen).
*
* A possible workaround is to handle this logic within the login screen, but for the sake of simplicity, this is not implemented in the sample.
*/
this.addEventListener("notificationclick", (event) => {
console.log("ServiceWorker Notification click: ", event);
event.preventDefault();
event.notification.close();
// See: https://github.com/airbnb/javascript/issues/1632
// eslint-disable-next-line no-restricted-globals
const url = new URL("./", self.location).href;
event.waitUntil(
clients
.matchAll({ type: "window", includeUncontrolled: true })
.then((clientList) => {
// If there are any clients open, just focus on it.
// Note that we can't focus directly when the push is received as some of the clients don't allow to focus a window
// without user interaction.
const clientToFocus = clientList.find((client) => "focus" in client);
if (clientToFocus) {
return clientToFocus.focus();
}
// If there are not opened clients, open a new one and post message that containts incoming call payload.
if (clients.openWindow) {
return clients
.openWindow(url)
.then(() => delay(1000))
.then(() => postMessageToClients(event.notification.data.body));
}
return Promise.resolve();
}),
);
});