blob: 92f2ac8284401ae6d1f30239c3f4543da37d02c8 (
plain)
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
|
/**
* Service Worker
*/
import composeNotification from './common/scripts/compose-notification';
// インストールされたとき
self.addEventListener('install', ev => {
console.info('installed');
ev.waitUntil(Promise.all([
self.skipWaiting(), // Force activate
]));
});
// プッシュ通知を受け取ったとき
self.addEventListener('push', ev => {
// クライアント取得
ev.waitUntil(self.clients.matchAll({
includeUncontrolled: true
}).then(clients => {
// クライアントがあったらストリームに接続しているということなので通知しない
if (clients.length != 0) return;
const { type, body } = ev.data.json();
const n = composeNotification(type, body);
return self.registration.showNotification(n.title, {
body: n.body,
icon: n.icon,
});
}));
});
|