-
Notifications
You must be signed in to change notification settings - Fork 18
/
firebase-messaging-sw.js
132 lines (115 loc) · 3.46 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
self.addEventListener('push', function (event) {
console.log('Received a push message', event);
var notification = event.data.json().notification
console.log(notification)
var title = notification.title || 'Yay a message.';
var body = notification.body || 'We have received a push message.';
var icon = notification.icon
// var tag = 'simple-push-demo-notification-tag';
event.waitUntil(
self.registration.showNotification(title, {
body: body,
icon : icon,
vibrate: [200, 100, 200, 100, 200, 100, 200]
// tag: tag
})
);
});
// on Notification Click do whatever you want...
self.addEventListener('notificationclick', function(event) {
var notification = event.notification;
var action = event.action;
console.log(notification);
if (action === 'confirm') {
console.log('Confirm was chosen');
notification.close();
} else {
console.log(action);
event.waitUntil(
clients.matchAll()
.then(function(clis) {
var client = clis.find(function(c) {
return c.visibilityState === 'visible';
});
if (client !== undefined) {
client.navigate("https://pakistan-olx-1.firebaseapp.com/Message/messageMain.html");
client.focus();
} else {
clients.openWindow("https://pakistan-olx-1.firebaseapp.com/Message/messageMain.html");
}
notification.close();
})
);
}
});
var cacheName = "OLX-Pak ";
var filesToCache = [
"/",
"/index.html",
"/css/bootstrap.min.css",
"/css/bootstrap.css",
"/js/bootstrap.js",
"/app.js",
"/style.css",
"/loader.gif",
"/Images/animals.png",
"/Images/user.png",
"/Images/mobile.jpg",
"/Images/logo.png",
"/fonts/font.woff2",
"/Login/login.css",
"/Login/login.html",
"/Login/login.js",
"/Message/message.css",
"/Message/message.html",
"/Message/message.js",
"/Message/messageMain.html",
"/My account/account.css",
"/My account/account.html",
"/My account/account.js",
"/Register/register.css",
"/Register/register.html",
"/Register/register.js",
"/Submit Add/ad.css",
"/Submit Add/ad.html",
"/Submit Add/ad.js",
]
self.addEventListener("install", function(e) {
console.log("[ServiceWorker] Install");
e.waitUntil(
caches.open(cacheName).then(function(cache) {
console.log("[ServiceWorker] Caching app shell");
return cache.addAll(filesToCache);
})
);
});
self.addEventListener("activate", function(e) {
console.log("[ServiceWorker] Activate");
e.waitUntil(
caches.keys().then(function(keyList) {
return Promise.all(
keyList.map(function(key) {
if (key !== cacheName) {
console.log("[ServiceWorker] Removing old cache", key);
return caches.delete(key);
}
})
);
})
);
return self.clients.claim();
});
self.addEventListener("fetch", function(e) {
console.log("[Service Worker] Fetch", e.request.url);
var dataUrl = "https://query.yahooapis.com/v1/public/yql";
/*
* The app is asking for app shell files. In this scenario the app uses the
* "Cache, falling back to the network" offline strategy:
* https://jakearchibald.com/2014/offline-cookbook/#cache-falling-back-to-network
*/
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request);
})
);
});