-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase-messaging-sw.js
51 lines (50 loc) · 1.9 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
importScripts("https://www.gstatic.com/firebasejs/7.16.1/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/7.16.1/firebase-messaging.js");
importScripts("https://www.gstatic.com/firebasejs/7.16.1/firebase-analytics.js");
firebase.initializeApp({
apiKey: "AIzaSyCuB53gY090zhFPT48Dd3AjcJ90WhfclHM",
authDomain: "bytelover-android.firebaseapp.com",
projectId: "bytelover-android",
storageBucket: "bytelover-android.appspot.com",
messagingSenderId: "228849322669",
appId: "1:228849322669:web:076f1f15909d03824dd424",
measurementId: "G-YBPDFVENSM"
});
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
icon: payload.notification.icon,
data: payload.data
};
return self.registration.showNotification(notificationTitle, notificationOptions);
});
self.addEventListener('notificationclick', function(event) {
const d = event.notification.data;
if (d && d.url) {
event.waitUntil(
clients.matchAll({type: 'window'}).then( windowClients => {
// Check if there is already a window/tab open with the target URL
for (var i = 0; i < windowClients.length; i++) {
var client = windowClients[i];
// If so, just focus it.
if (client.url === d.url && 'focus' in client) {
return client.focus();
}
}
// If not, then open the target URL in a new window/tab.
if (clients.openWindow) {
return clients.openWindow(d.url);
}
})
)
}
event.notification.close();
});
self.addEventListener('activate', function(event) {
event.waitUntil(self.clients.claim());
});
self.addEventListener('install', function(event) {
event.waitUntil(self.skipWaiting());
});