-
Notifications
You must be signed in to change notification settings - Fork 0
/
serviceworker.js
45 lines (38 loc) · 1.08 KB
/
serviceworker.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
CACHE_NAME = "default-v1";
CACHE_ASSETS = [];
TAG = "[SW]: ";
self.addEventListener("install", event => {
console.log(TAG, "installing");
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
cache.addAll(CACHE_ASSETS)
})
)
})
self.addEventListener("activate", event => {
console.log(TAG, "activated");
})
self.addEventListener("fetch", event => {
console.log(TAG, "fetching");
event.respondWith(fetch(event.request));
})
self.addEventListener("push", event => {
console.log(TAG, "push message recived");
event.waitUntil(new Promise(function(resolve, reject){
if (!(self.Notification && self.Notification.permission === 'granted')) {
return;
}
var data = {};
if (event.data) {
try{
data = event.data.json();
}catch(error){
data={}
}
}
var title = data.title || "Nuova notifica";
var message = data.message || "Questo e solo un messaggio fisso.";
var options= { body: message, tag: "simple-1"}
var myNotification = self.registration.showNotification(title, options).then(resolve());
}))
})