-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfirebase-messaging-sw.js.example
47 lines (40 loc) · 1.35 KB
/
firebase-messaging-sw.js.example
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
// Scripts for firebase and firebase messaging
importScripts('https://www.gstatic.com/firebasejs/9.21.0/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/9.21.0/firebase-messaging-compat.js');
// Initialize the Firebase app in the service worker by passing the generated config
// TO-DO: setup
var firebaseConfig = {
apiKey: 'api-key',
authDomain: 'project-id.firebaseapp.com',
projectId: 'project-id',
storageBucket: 'project-id.appspot.com',
messagingSenderId: 'sender-id',
appId: 'app-id',
measurementId: 'G-measurement-id',
};
firebase.initializeApp(firebaseConfig);
// Retrieve firebase messaging
const messaging = firebase.messaging();
messaging.onBackgroundMessage(function (payload) {
console.log('Received background message ', payload);
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
actions: [
{ action: 'view', title: 'View' },
{ action: 'close', title: 'Close' }
],
data: {
url: payload.fcmOptions.link
},
// icon: payload.data.icon,
// silent: true
};
self.registration.showNotification(notificationTitle, notificationOptions);
});
self.addEventListener('notificationclick', (e) => {
let payload = e.notification
if (e.action === 'view') {
clients.openWindow(payload.data.url)
}
})