-
-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathservice-worker.js
executable file
·45 lines (37 loc) · 1.08 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
'use strict';
/* eslint-env browser, serviceworker */
self.addEventListener('install', () => {
self.skipWaiting();
});
self.addEventListener('push', function(event) {
console.log('Push message received.');
let notificationTitle = 'Hello';
const notificationOptions = {
body: 'Thanks for sending this push msg.',
icon: './images/logo-192x192.png',
badge: './images/badge-72x72.png',
data: {
url: 'https://web.dev/push-notifications-overview/',
},
};
if (event.data) {
const dataText = event.data.text();
notificationTitle = 'Received Payload';
notificationOptions.body = `Push data: '${dataText}'`;
}
event.waitUntil(
self.registration.showNotification(
notificationTitle,
notificationOptions,
),
);
});
self.addEventListener('notificationclick', function(event) {
console.log('Notification clicked.');
event.notification.close();
let clickResponsePromise = Promise.resolve();
if (event.notification.data && event.notification.data.url) {
clickResponsePromise = clients.openWindow(event.notification.data.url);
}
event.waitUntil(clickResponsePromise);
});