-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebtexting-worker.js
More file actions
62 lines (53 loc) · 2.08 KB
/
webtexting-worker.js
File metadata and controls
62 lines (53 loc) · 2.08 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
const channel = new BroadcastChannel('message-pushes');
console.log("channel state", channel);
console.log("self.clients", self.clients);
self.addEventListener('install', function (event) {
event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', function (event) {
event.waitUntil(self.clients.claim());
});
// Register event listener for the 'push' event.
self.addEventListener('push', function (event) {
event.waitUntil(
// Retrieve a list of the clients of this service worker.
self.clients.matchAll().then(function (clientList) {
console.log(clientList, event);
const payload = event.data.json();
channel.postMessage(payload);
var tab = clientList.some(function (client) {
let u = new URL(client.url);
console.log("push return info", u.searchParams.get('extension_uuid') == payload.to && u.searchParams.get('number') == payload.from)
return u.searchParams.get('extension_uuid') == payload.to && u.searchParams.get('number') == payload.from;
});
console.log("tab", tab)
if(tab) {
console.log("tab with conversation exists");
if(tab.focused) {
console.log('tab with conversation is focused, not sending notification');
return;
}
}
return self.registration.showNotification(payload.display_name, {body: payload.body});
})
);
});
self.addEventListener('notificationclick', function (event) {
event.notification.close();
event.waitUntil(
self.clients.matchAll().then(function (clientList) {
var tab = clientList.some(function (client) {
let u = new URL(client.url);
return u.searchParams.get('extension_uuid') == payload.to && u.searchParams.get('number') == payload.from;
});
if(tab) {
tab.focus();
} else {
let u = self.origin + '/app/webtexting/thread.php?extension_uuid=' + payload.to + '&number=' + payload.from;
console.log('opening URL:', u);
self.clients.openWindow(u)
.then((windowClient) => (windowClient ? windowClient.focus() : null));
}
})
);
});