This commit is contained in:
syuilo 2017-11-21 07:06:36 +09:00
parent cad1e0458f
commit 411c006d9f
2 changed files with 103 additions and 42 deletions

View file

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