Firebase Messaging WIP to clean up service worker and implement proper notifications.
Missed in last commit
This commit is contained in:
@@ -28,29 +28,70 @@ firebase.initializeApp({
|
||||
const messaging = firebase.messaging();
|
||||
|
||||
//Handles Background Messages
|
||||
// messaging.setBackgroundMessageHandler(function (payload) {
|
||||
// console.log("backgroundMessageHandler", payload);
|
||||
// const promiseChain = clients
|
||||
// .matchAll({
|
||||
// type: "window",
|
||||
// includeUncontrolled: true,
|
||||
// })
|
||||
// .then((windowClients) => {
|
||||
// for (let i = 0; i < windowClients.length; i++) {
|
||||
// const windowClient = windowClients[i];
|
||||
// console.log("[fbmsw] Posting Paylout to window client.");
|
||||
// windowClient.postMessage(payload);
|
||||
// }
|
||||
// })
|
||||
// .then(() => {
|
||||
// console.log("[fbmsw] Showing notification.");
|
||||
// return registration.showNotification(JSON.stringify(payload));
|
||||
// });
|
||||
// return promiseChain;
|
||||
// });
|
||||
|
||||
// messaging.onMessage((payload) => {
|
||||
// console.log("Message received. ", payload);
|
||||
// // ...
|
||||
// });
|
||||
|
||||
messaging.setBackgroundMessageHandler(function (payload) {
|
||||
console.log("backgroundMessageHandler", payload);
|
||||
const promiseChain = clients
|
||||
.matchAll({
|
||||
type: "window",
|
||||
includeUncontrolled: true,
|
||||
})
|
||||
.then((windowClients) => {
|
||||
for (let i = 0; i < windowClients.length; i++) {
|
||||
const windowClient = windowClients[i];
|
||||
windowClient.postMessage(payload);
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
return registration.showNotification(JSON.stringify(payload));
|
||||
});
|
||||
return promiseChain;
|
||||
console.log(
|
||||
"**********[firebase-messaging-sw.js] Received background message ",
|
||||
payload
|
||||
);
|
||||
// Customize notification here
|
||||
const notificationTitle = "Background Message Title";
|
||||
const notificationOptions = {
|
||||
body: payload.notification.body + "FROM SW",
|
||||
icon: "logo240.png",
|
||||
};
|
||||
|
||||
return self.registration.showNotification(
|
||||
notificationTitle,
|
||||
notificationOptions
|
||||
);
|
||||
});
|
||||
|
||||
// self.addEventListener("message", (message) => {
|
||||
// const { payload } = message.data.firebaseMessaging;
|
||||
|
||||
// navigator.serviceWorker.getRegistration().then((registration) =>
|
||||
// registration.showNotification(payload.notification.title, {
|
||||
// body: payload.notification.body + "FROM SW",
|
||||
// icon: "logo240.png",
|
||||
// badge: "logo240.png",
|
||||
// actions: [
|
||||
// {
|
||||
// action: "respond",
|
||||
// title: "Respond",
|
||||
// },
|
||||
// ],
|
||||
// })
|
||||
// );
|
||||
// });
|
||||
|
||||
//Handles the notification getting clicked.
|
||||
self.addEventListener("notificationclick", function (event) {
|
||||
// do what you want
|
||||
// ...
|
||||
console.log("SW notificationclick", event);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user