-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
148 lines (132 loc) · 4.2 KB
/
sw.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
console.log('SERVICE WORKER');
importScripts('/js/polyfill.min.js');
importScripts('/js/idb.js');
let visibilityState = 'visible';
const cachedDatabase = idb.openDB('telekram', 5, {
upgrade: (db, oldVersion, newVersion) => {
const tables = ['profilePhotos', 'chatPreferences', 'mediaAttachments', 'offlineWebpages', 'appPreferences'];
tables.forEach(n => {
if (!db.objectStoreNames.contains(n))
db.createObjectStore(n);
});
},
});
self.addEventListener('install', (event) => {
console.log('[SW] on install');
event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', (event) => {
console.log('[SW] on activate');
event.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', (event) => {
// console.log('[SW] on fetch');
});
self.addEventListener('push', (event) => {
const obj = event.data.json();
let hasActiveWindows = false;
console.log('[SW] on push', obj);
clients.matchAll({type: 'window'})
.then((clientList) => {
console.log('matched clients', clientList)
hasActiveWindows = clientList.length > 0;
if (visibilityState !== 'visible') {
fireNotification(obj);
return;
}
if (hasActiveWindows) {
console.log('Supress notification because some instance is alive')
return;
}
fireNotification(obj);
})
.catch((err) => {
console.log(err);
});
});
self.addEventListener('message', (event) => {
console.log('[SW] on message:', event.data);
switch (event.data.type) {
case 0: // clearNotification
break;
case 1: // visibilityState
visibilityState = event.data.visibilityState;
console.log('visibilityState :', visibilityState);
break;
}
});
self.addEventListener('notificationclick', (event) => {
console.log('[SW] on notificationclick');
event.notification.close();
event.waitUntil(clients.matchAll({ type: "window" })
.then((clientList) => {
for (var i = 0; i < clientList.length; i++) {
let client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow) {
return clients.openWindow('/');
}
if (clients.openApp) {
return clients.openApp();
}
})
.catch((err) => {
console.log(err);
}));
});
self.addEventListener('notificationclose', (event) => {
console.log('[SW] on notificationclose');
});
// https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/pushsubscriptionchange_event
self.addEventListener('pushsubscriptionchange', (event) => {
console.log('[SW] on pushsubscriptionchange', event);
const result = self.registration.pushManager.subscribe({userVisibleOnly: true})
.then((subscription) => {
const subscriptionObj = subscription.toJSON();
delete subscriptionObj['expirationTime'];
return Promise.all([cachedDatabase, Promise.resolve(subscriptionObj)]);
})
.then(values => {
return values[0].put('appPreferences', values[1], 'updatedPushSubscription');
})
.then(key => {
console.log('[SW]@pushsubscriptionchange:', key);
fireSystemNotification('System Update', 'Please re-launch app to apply new push notification subscription');
})
.catch(err => {
console.error('[SW]@pushsubscriptionchange:', err);
fireSystemNotification('System Update', 'Unable to resubscribe for push notification');
});
event.waitUntil(result);
});
function fireSystemNotification(title, body, requireInteraction = false, actions = []) {
self.registration.showNotification(title || 'Telegram', {
body: body || 'Hello from Telegram',
requireInteraction: requireInteraction,
actions: actions
});
}
function fireNotification(obj) {
let title = obj.title || 'Telegram'
let body = obj.description || ''
let icon = '/icons/icon56x56.png'
let peerID
if (obj.custom && obj.custom.channel_id) {
peerID = -obj.custom.channel_id
} else if (obj.custom && obj.custom.chat_id) {
peerID = -obj.custom.chat_id
} else {
peerID = obj.custom && obj.custom.from_id || 0
}
obj.custom.peerID = peerID
let tag = 'peer' + peerID
console.log('[SW] show notify', title, body, icon, obj)
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag,
data: obj,
});
}