forked from autonome/push-api-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
30 lines (25 loc) · 705 Bytes
/
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
var port;
self.addEventListener('push', function(event) {
var obj = event.data.json();
if(obj.action === 'subscribe' || obj.action === 'unsubscribe') {
fireNotification(obj, event);
port.postMessage(obj);
} else if(obj.action === 'init' || obj.action === 'chatMsg') {
port.postMessage(obj);
}
});
self.onmessage = function(e) {
console.log(e);
port = e.ports[0];
}
function fireNotification(obj, event) {
var title = 'Subscription change';
var body = obj.name + ' has ' + obj.action + 'd.';
var icon = 'push-icon.png';
var tag = 'push';
event.waitUntil(self.registration.showNotification(title, {
body: body,
icon: icon,
tag: tag
}));
}