aboutsummaryrefslogtreecommitdiff
path: root/src/main/resources/static/sw.js
blob: 0520bcc171066a203ae952bcb7cf866e4e9919c0 (plain) (blame)
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
/// <reference no-default-lib="true"/>
/// <reference lib="esnext" />
/// <reference lib="WebWorker" />

var sw = /** @type {ServiceWorkerGlobalScope & typeof self} */ (self);
sw.addEventListener('install', function() {
  sw.skipWaiting();
});
sw.addEventListener('activate', function() {
  sw.clients.claim();
});
sw.addEventListener('push', function(evt) {
  if (evt.data) {
    const data = evt.data.json();
    console.log('Push', data);
    if (data.body) {
      sw.registration.showNotification(data.title, {
        body: data.body,
        icon: data.custom.message.user.avatar,
        tag: data.tag,
        data: data.custom.message
      });
    }
  }
});
sw.addEventListener('notificationclick', function(evt) {
  evt.waitUntil(
    sw.clients.matchAll().then(function(clientList) {
      const message = evt.notification.data;
      const url = message.mid
        ? message.rid
          ? `https://juick.com/m/${message.mid}#${message.rid}`
          : `https://juick.com/m/${message.mid}`
        : 'https://juick.com/pm/inbox';
      if (clientList.length > 0) {
        for (var i = 0; i < clientList.length; i++) {
          if (clientList[i].url === url) {
            return clientList[i].focus();
          }
        }
        clientList[0].navigate(url);
        return clientList[0].focus();
      }
      return sw.clients.openWindow(url);
    })
  );
});