-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfirebase-messaging-sw.js
59 lines (51 loc) · 1.67 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
52
53
54
55
56
57
58
59
importScripts('https://www.gstatic.com/firebasejs/10.1.0/firebase-app-compat.js');
importScripts('https://www.gstatic.com/firebasejs/10.1.0/firebase-messaging-compat.js');
console.log("in service worker")
var CACHE_NAME = 'my-site-cache-v1';
var urlsToCache = [
];
self.addEventListener('install', function(event) {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
if (response) {
return response;
}
return fetch(event.request).catch(function(error) {
console.error('Fetch failed:', error);
throw error;
});
})
);
});
const firebaseConfig = {
apiKey: "AIzaSyAj7iUJlTR_JDvq62rmfe6eZZXvtsO8Cac",
authDomain: "sturdy-analyzer-381018.firebaseapp.com",
projectId: "sturdy-analyzer-381018",
storageBucket: "sturdy-analyzer-381018.appspot.com",
messagingSenderId: "800350153500",
appId: "1:800350153500:web:3da9c736e97b9f582928d9",
measurementId: "G-TGW6CJ0H1Q"
};
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
// Handle background messages
messaging.onBackgroundMessage(function(payload) {
console.log('Received background message ', payload);
const notificationTitle = payload.notification.title;
const notificationOptions = {
body: payload.notification.body,
icon: '/static/media/favicon.png'
};
self.registration.showNotification(notificationTitle, notificationOptions);
});