forked from dentarg/auth0-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
33 lines (32 loc) · 1.06 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
var link;
self.addEventListener('push', function (event) {
event.waitUntil(
fetch('last.json').then(function (response) {
return response.json();
}).then(function(data){
link = data.link + '?utm_source=notifications-chrome&utm_medium=sc&utm_campaign=notifications';
return self.registration.showNotification('New article in Auth0 blog!', {
body: data.title,
icon: data.thumbnail,
tag: 'Auth0-blog-post-notification'
})
})
);
});
self.addEventListener('notificationclick', function(event) {
event.waitUntil(
clients.matchAll({
type: "window"
})
.then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client)
return client.focus();
}
if (clients.openWindow) {
return clients.openWindow(link);
}
})
);
});