-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice-worker.js
49 lines (43 loc) · 1.29 KB
/
service-worker.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
'use strict';
self.addEventListener('push', function(event) {
console.log('Received a push message', event);
var title = 'MyDeal';
var body = 'Free Shipping Friday - TVs & Projectors';
var icon = 'https://www.videopro.com.au/images/product/micro/8491_1.jpg';
var tag = 'Push Tag';
if (event.data) {
console.log(event.data.json());
title = event.data.json().title;
body = event.data.json().body;
icon = event.data.json().icon;
tag = event.data.json().tag;
}
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag
})
);
});
self.addEventListener('notificationclick', function(event) {
console.log('On notification click: ', event.notification.tag);
// Android doesn’t close the notification when you click on it
// See: http://crbug.com/463146
event.notification.close();
// This looks to see if the current is already open and
// focuses if it is
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('/');
}
}));
});