-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathfirebase-messaging-sw.js
49 lines (41 loc) · 1.36 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
importScripts('https://www.gstatic.com/firebasejs/4.3.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/4.3.1/firebase-messaging.js');
const msg_sender_id = '868637702480';
firebase.initializeApp({
'messagingSenderId': msg_sender_id
});
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function (message) {
self.clients.matchAll({
type: 'window',
includeUncontrolled: true
}).then(function (clients) {
clients.forEach(function (client) {
client.postMessage({
"firebase-messaging-msg-type": "push-msg-received",
"firebase-messaging-msg-data": message
});
})
});
if (message.from === msg_sender_id) {
var title, body,
icon = 'images/xabber-logo-48.png';
var payload;
try {
payload = JSON.parse(atob(message.data.body));
} catch (e) {
payload = message.data
}
if (payload.action === 'settings_updated') {
title = 'Settings updated';
body = 'for user ' + payload.username;
return self.registration.showNotification(title, {
body: body,
icon: icon
});
}
}
});
self.addEventListener('notificationclick', function (event) {
event.notification.close();
});