-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase-sw.js
45 lines (36 loc) · 1.39 KB
/
firebase-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
// Scripts for firebase and firebase messaging
importScripts('https://www.gstatic.com/firebasejs/9.6.0/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/9.6.0/firebase-messaging-compat.js');
importScripts('https://www.gstatic.com/firebasejs/9.6.0/firebase-auth-compat.js');
//V8 for comparison
//importScripts('https://www.gstatic.com/firebasejs/8.10.0/firebase-auth.js');
// Initialize the Firebase app in the service worker by passing the generated config
var firebaseConfig = {
....
};
firebase.initializeApp(firebaseConfig);
// Retrieve firebase messaging
const messaging = firebase.messaging();
messaging.getToken({ vapidKey: 'VAPID_KEY' })
.then(result =>{
console.log(result);
});
messaging.onBackgroundMessage(function (payload) {
console.log('Received background message ', payload);
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
};
self.registration.showNotification(notificationTitle,
notificationOptions);
});
auth.onAuthStateChanged((user) => {
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/firebase.User
console.log('Webworker: ',user.uid)
} else {
// User is signed out
console.log("Webworker: User is signed out");
}
})