-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase-messaging-sw.js
102 lines (92 loc) · 3.14 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Import and configure the Firebase SDK
// These scripts are made available when the app is served or deployed on Firebase Hosting
// If you do not serve/host your project using Firebase Hosting see https://firebase.google.com/docs/web/setup
// importScripts('/__/firebase/7.6.0/firebase-app.js');
// importScripts('/__/firebase/7.6.0/firebase-messaging.js');
// importScripts('/__/firebase/init.js');
// const messaging = firebase.messaging();
// [START initialize_firebase_in_sw]
// 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/7.6.0/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/7.6.0/firebase-messaging.js");
self.addEventListener("notificationclick", event => {
console.log("notificationclicked ");
event.notification.close();
console.log(event.notification);
const { data = {} } = event.notification;
var temp = "/";
var urlToOpen = new URL(temp, self.location.origin).href;
console.log("urlToOpen: ", urlToOpen);
var promiseChain = clients
.matchAll({
type: "window",
includeUncontrolled: true
})
.then(function(windowClients) {
let matchingClient = null;
for (var i = 0; i < windowClients.length; i++) {
var windowClient = windowClients[i];
if (windowClient.url === urlToOpen) {
matchingClient = windowClient;
break;
}
}
if (matchingClient && "focus" in matchingClient) {
return matchingClient.focus();
} else if ("openWindow" in clients) {
const openWindowPromise = clients.openWindow(urlToOpen);
return openWindowPromise
.then(() => {
console.log("openned");
return true;
})
.catch(err => {
console.log(err);
});
}
});
event.waitUntil(
promiseChain.then(() => {
console.log("promise_ended");
})
);
});
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
const firebaseConfig = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: "",
appId: ""
};
firebase.initializeApp(firebaseConfig);
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
// [END initialize_firebase_in_sw]
// If you would like to customize notifications that are received in the
// background (Web app is closed or not in browser focus) then you should
// implement this optional method.
// [START background_handler]
messaging.setBackgroundMessageHandler(function(payload) {
console.log(
"[firebase-messaging-sw.js] Received background message ",
payload
);
// Customize notification here
const notificationTitle = "Background Message Title";
const notificationOptions = {
body: "Background Message body.",
icon: "/firebase-logo.png"
};
return self.registration.showNotification(
notificationTitle,
notificationOptions
);
});
// [END background_handler]