fix: duplicated notification

This commit is contained in:
zerosoul
2022-04-08 20:19:39 +08:00
parent 8aed9c9ff4
commit 8a8678eef2
7 changed files with 113 additions and 171 deletions
+6 -39
View File
@@ -2,8 +2,12 @@
// See `Access registration token section` @ https://firebase.google.com/docs/cloud-messaging/js/client#retrieve-the-current-registration-token
// Scripts for firebase and firebase messaging
importScripts("https://www.gstatic.com/firebasejs/8.2.0/firebase-app.js");
importScripts("https://www.gstatic.com/firebasejs/8.2.0/firebase-messaging.js");
importScripts(
"https://www.gstatic.com/firebasejs/9.6.10/firebase-app-compat.js"
);
importScripts(
"https://www.gstatic.com/firebasejs/9.6.10/firebase-messaging-compat.js"
);
// Initialize the Firebase app in the service worker by passing the generated config
const firebaseConfig = {
@@ -34,22 +38,6 @@ messaging.onBackgroundMessage((payload) => {
self.registration.showNotification(notificationTitle, notificationOptions);
});
// setTimeout(() => {
// console.log("notification test");
// self.registration.showNotification("hello", { body: "test" });
// }, 5000);
// 开始监听推送
// self.addEventListener("push", function (event) {
// var data = event.data.json();
// const title = data.Title;
// data.Data.actions = data.Actions;
// const options = {
// body: data.Message,
// data: data.Data,
// };
// event.waitUntil(self.registration.showNotification(title, options));
// });
self.addEventListener("notificationclick", function (event) {
console.log("notification click", event, event.notification);
@@ -86,27 +74,6 @@ self.addEventListener("notificationclick", function (event) {
firstClient.postMessage({ newPath: redirectPath });
firstClient.focus();
}
// // Let's see if we already have a chat window open:
// for (const client of allClients) {
// const url = new URL(client.url);
// if (url.pathname == '/chat/') {
// // Excellent, let's use it!
// client.focus();
// chatClient = client;
// break;
// }
// }
// // If we didn't find an existing chat window,
// // open a new one:
// if (!chatClient) {
// chatClient = await clients.openWindow('/chat/');
// }
// // Message the client:
// chatClient.postMessage("New chat messages!");
})()
);
});