-
Notifications
You must be signed in to change notification settings - Fork 1
/
firebase-messaging-sw.js
82 lines (74 loc) · 2 KB
/
firebase-messaging-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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
self.addEventListener('push', function (event) {
var notification = event.data.json().notification
var title = notification.title;
var body = notification.body;
var url = notification.click_action;
var icon = '../images/icons/icon-128x128.png';
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon: icon,
data: url
})
);
});
self.addEventListener('notificationclick', function (event) {
console.log('On notification click: ', event.notification);
event.notification.close();
clients.openWindow(event.notification.data);
});
var dataCacheName = 'OLXData';
var cacheName = 'PWA';
var filesToCache = [
'/',
'/index.html',
'/layout/ad.html',
'/layout/ads.html',
'/layout/category.html',
'/layout/edit.html',
'/layout/fav-detail.html',
'/layout/fav.html',
'/layout/inbox.html',
'/layout/login.html',
'/layout/message.html',
'/layout/offlineAd.html',
'/layout/post.html',
'/layout/register.html',
'/layout/search.html',
'/layout/user.html',
'/js/index.js',
'/css/all.css',
'/css/index.css',
'images/download.jpg',
'images/loading.gif',
'images/offline.png',
'images/icons/icon-128x128.png'
];
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(cacheName).then(function(cache) {
console.log('[ServiceWorker] Caching app shell');
return cache.addAll(filesToCache);
})
);
});
self.addEventListener('activate', function(e) {
e.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(keyList.map(function(key) {
if (key !== cacheName && key !== dataCacheName) {
console.log('[ServiceWorker] Removing old cache', key);
return caches.delete(key);
}
}));
})
);
return self.clients.claim();
});
self.addEventListener('fetch', function(event) {
event.respondWith(
fetch(event.request).catch(function() {
return caches.match(event.request);
})
);
});