-
Notifications
You must be signed in to change notification settings - Fork 91
/
firebase-messaging-sw.js
39 lines (31 loc) · 1.21 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
importScripts('https://www.gstatic.com/firebasejs/3.7.2/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/3.7.2/firebase-messaging.js');
firebase.initializeApp({
messagingSenderId: '448358493027'
});
const messaging = firebase.messaging();
// Customize notification handler
messaging.setBackgroundMessageHandler(function(payload) {
console.log('Handling background message', payload);
// Copy data object to get parameters in the click handler
payload.data.data = JSON.parse(JSON.stringify(payload.data));
return self.registration.showNotification(payload.data.title, payload.data);
});
self.addEventListener('notificationclick', function(event) {
const target = event.notification.data.click_action || '/';
event.notification.close();
// This looks to see if the current is already open and focuses if it is
event.waitUntil(clients.matchAll({
type: 'window',
includeUncontrolled: true
}).then(function(clientList) {
// clientList always is empty?!
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url === target && 'focus' in client) {
return client.focus();
}
}
return clients.openWindow(target);
}));
});