-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase-messaging-sw.js
51 lines (44 loc) · 1.84 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
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here. Other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp({
apiKey: "AIzaSyBVLFLSTaIBGVUDeYyTNMt9I69TsaozKsA",
authDomain: "foreveryou-27e01.firebaseapp.com",
projectId: "foreveryou-27e01",
storageBucket: "foreveryou-27e01.appspot.com",
messagingSenderId: "198719782417",
appId: "1:198719782417:web:6432ddad3fa200e287fbb0",
measurementId: "G-9XLT9434GP"
});
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.onBackgroundMessage((payload) => {
console.log(
'[firebase-messaging-sw.js] Received background message ',
payload
);
// Customize notification here
const paramName = payload.data.action == 'sending' ? 'from' : 'action';
const destinationUrl = self.location.protocol + '//' + self.location.host + '/chat.html?' + paramName + '=' + payload.data.fromId
const notificationTitle = 'You got a new message on ForeverYou.com';
const notificationOptions = {
body: payload.data.body,
data: { url: destinationUrl }, //the url which we gonna use later
actions: [{action: "open_url", title: "Read now"}]
};
self.registration.showNotification(notificationTitle, notificationOptions);
});
self.addEventListener('notificationclick', function(event) {
switch(event.action){
case 'open_url':
clients.openWindow(event.notification.data.url);
break;
}
}
, false);