This commit is contained in:
syuilo 2017-11-21 05:09:45 +09:00
parent 3fa7215f39
commit c598a9cba2
7 changed files with 91 additions and 31 deletions

30
src/web/app/sw.js Normal file
View file

@ -0,0 +1,30 @@
/**
* Service Worker
*/
import composeNotification from './common/scripts/compose-notification';
// インストールされたとき
self.addEventListener('install', () => {
console.log('[sw]', 'Your ServiceWorker is installed');
});
// プッシュ通知を受け取ったとき
self.addEventListener('push', ev => {
// クライアント取得
self.clients.matchAll({
includeUncontrolled: true
}).then(clients => {
// クライアントがあったらストリームに接続しているということなので通知しない
if (clients.length != 0) return;
const { type, body } = ev.data.json();
const n = composeNotification(type, body);
if (n) {
self.registration.showNotification(n.title, {
body: n.body,
icon: n.icon,
});
}
});
});