-
Notifications
You must be signed in to change notification settings - Fork 40
/
firebase-messaging-sw.js
44 lines (39 loc) · 1.43 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
importScripts('./js/vendor/firebase-app.js');
importScripts('./js/vendor/firebase-messaging.js');
firebase.initializeApp({
messagingSenderId: "1020521924918",
projectId: 'pocketnet',
apiKey: 'AIzaSyC_Jeet2gpKRZp44iATwlFFA7iGNYsabkk',
appId: '1:1020521924918:ios:ab35cc84f0d10d86aacb97',
});
const messaging = firebase.messaging();
const url = new URL(self.serviceWorker?.scriptURL || "");
messaging.onBackgroundMessage(function(payload) {
const notification = payload?.data?.json ? JSON.parse(payload?.data?.json) : {};
const notificationTitle = notification?.header?.title;
const notificationOptions = {
body: notification?.header?.body,
icon: notification?.image || `${url.origin}/img/logo_color/blue_250.png`,
data: notification,
};
return self.registration.showNotification(
notificationTitle,
notificationOptions,
);
});
self.addEventListener('notificationclick', function(event) {
const notificationUrl = url.origin + (event?.notification?.data?.url || `/userpage?id=notifications&report=notifications`)
event.notification.close();
event.waitUntil(clients.matchAll({
type: 'window',
includeUncontrolled: true
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url === notificationUrl && 'focus' in client) {
return client.focus();
}
}
return clients.openWindow(notificationUrl);
}));
});