-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysw.js
29 lines (25 loc) · 1.21 KB
/
mysw.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
//this code was lifted from : https://developers.google.com/web/fundamentals/getting-started/codelabs/push-notifications/
//add i believe it belongs to GOOGLE
'use strict';
self.addEventListener('push', function(event) {
console.log('[Service Worker] Push Received.');
console.log(`[Service Worker] Push had this data: `);
console.log(JSON.parse(event.data.text()));//modified from tutorial to make it more dynamic
const notificationObject = JSON.parse(event.data.text());//modified from tutorial to make it more dynamic
const title = notificationObject.title;//modified from tutorial to make it more dynamic
const options = {
body: notificationObject.msg,
icon: notificationObject.icon,
badge: notificationObject.badge
};
self.notificationURL = notificationObject.url;//modified from tutorial to make it more dynamic
event.waitUntil(self.registration.showNotification(title, options));
});
self.addEventListener('notificationclick', function(event) {
console.log('[Service Worker] Notification click Received.');
//console.log(self.notificationURL);
event.notification.close();
event.waitUntil(
clients.openWindow(self.notificationURL)//modified from tutorial to make it more dynamic
);
});