-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
147 lines (132 loc) · 5.2 KB
/
sw.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
(function () {
'use strict'
let staticCacheName = `pwa-v${new Date().getTime()}`
const WebPush = {
_filesToCache: [
'/media/pwa/icons/icon-72x72.png',
'/media/pwa/icons/icon-96x96.png',
'/media/pwa/icons/icon-128x128.png',
'/media/pwa/icons/icon-144x144.png',
'/media/pwa/icons/icon-152x152.png',
'/media/pwa/icons/icon-192x192.png',
'/media/pwa/icons/icon-384x384.png',
'/media/pwa/icons/icon-512x512.png',
'/media/pwa/splash/splash-640x1136.png',
'/media/pwa/splash/splash-750x1334.png',
'/media/pwa/splash/splash-1242x2208.png',
'/media/pwa/splash/splash-1125x2436.png',
'/media/pwa/splash/splash-828x1792.png',
'/media/pwa/splash/splash-1242x2688.png',
'/media/pwa/splash/splash-1536x2048.png',
'/media/pwa/splash/splash-1668x2224.png',
'/media/pwa/splash/splash-1668x2388.png',
'/media/pwa/splash/splash-2048x2732.png'
],
init() {
self.addEventListener('install', this.serviceWorkerInstalled.bind(this))
self.addEventListener('activate', this.serviceWorkerActivated.bind(this))
self.addEventListener('fetch', this.serviceWorkerFetching.bind(this))
self.addEventListener('push', this.notificationPush.bind(this))
self.addEventListener('notificationclick', this.notificationClick.bind(this))
self.addEventListener('notificationclose', this.notificationClose.bind(this))
},
serviceWorkerInstalled(event) {
console.log('Installed')
self.skipWaiting()
event.waitUntil(caches.open(staticCacheName).then(cache => cache.addAll(this._filesToCache)))
},
serviceWorkerActivated(event) {
event.waitUntil(
caches.keys().then(cacheNames => Promise.all(cacheNames
.filter(cacheName => (cacheName.startsWith('pwa')))
.filter(cacheName => (cacheName !== staticCacheName))
.map(cacheName => caches.delete(cacheName))))
)
},
serviceWorkerFetching(event) {
event.respondWith(
caches.match(event.requests)
.then(response => response || fetch(event.request))
.catch(() => caches.match('offline'))
)
},
/**
* Handle notification push event.
*
* https://developer.mozilla.org/en-US/docs/Web/Events/push
*
* @param {NotificationEvent} event
*/
notificationPush(event) {
console.log(self.Notification, self.Notification.permission)
if (!(self.Notification && self.Notification.permission === 'granted')) {
// Notifications are not supported or permission is denied
return;
}
if (event.data) {
console.log('Notification received', event.data)
event.waitUntil(
this.sendNotification(event.data.json())
)
}
},
/**
* Handle notification click event.
*
* https://developer.mozilla.org/en-US/docs/Web/Events/notificationclick
*
* @param {NotificationEvent} event
*/
notificationClick(event) {
console.log('Clicked', event)
if (event.action === 'some_action') {
// Do something...
} else {
self.clients.openWindow('/')
}
},
/**
* Handle notification close event (Chrome 50+, Firefox 55+).
*
* https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/onnotificationclose
*
* @param {NotificationEvent} event
*/
notificationClose(event) {
self.registration.pushManager.getSubscription().then(subscription => {
if (subscription) {
this.dismissNotification(event, subscription)
}
})
},
/**
* Send notification to the user.
*
* https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification
*
* @param {PushMessageData|Object} data
*/
sendNotification(data,) {
return self.registration.showNotification(data.title, data)
},
/**
* Send request to server to dismiss a notification.
*
* @param {NotificationEvent} event
* @param {String} subscription.endpoint
* @return {Response}
*/
dismissNotification({notification}, {endpoint}) {
console.log('Dismissed')
if (!notification.data || !notification.data.id) {
return;
}
// Send a request to the server to mark the notification as read.
fetch(`/notifications/${notification.data.id}/dismiss`, {
body: {endpoint: endpoint},
method: 'POST',
}).then(resp => resp.json()).then(console.log)
}
}
WebPush.init()
})()