egirlskey/src/web/app/sw.js

31 lines
781 B
JavaScript
Raw Normal View History

2017-11-20 18:40:09 +00:00
/**
* Service Worker
*/
2017-11-20 20:09:45 +00:00
import composeNotification from './common/scripts/compose-notification';
2017-11-20 18:40:09 +00:00
// インストールされたとき
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();
2017-11-20 20:09:45 +00:00
const n = composeNotification(type, body);
if (n) {
self.registration.showNotification(n.title, {
body: n.body,
icon: n.icon,
});
}
2017-11-20 18:40:09 +00:00
});
});