fix: notification click router

This commit is contained in:
Tristan Yang
2023-04-09 10:27:53 +08:00
parent cf28d56653
commit aea8023af7
2 changed files with 17 additions and 16 deletions
+2 -1
View File
@@ -27,7 +27,8 @@ self.addEventListener("notificationclick", function (event) {
return; return;
} }
if (allClients.length == 0) { if (allClients.length == 0) {
chatClient = await clients.openWindow(redirectPath); // hash路由
chatClient = await clients.openWindow(`/#${redirectPath}`);
} else { } else {
firstClient.postMessage({ newPath: redirectPath }); firstClient.postMessage({ newPath: redirectPath });
firstClient.focus(); firstClient.focus();
+15 -15
View File
@@ -1,12 +1,12 @@
import { useEffect, memo } from "react"; import { useEffect, memo } from "react";
// import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import useDeviceToken from "./useDeviceToken"; import useDeviceToken from "./useDeviceToken";
import { vapidKey } from "../../../app/config"; import { vapidKey } from "../../../app/config";
import { useUpdateDeviceTokenMutation } from "../../../app/services/auth"; import { useUpdateDeviceTokenMutation } from "../../../app/services/auth";
let updated = false; let updated = false;
let updating = false; let updating = false;
const Notification = () => { const Notification = () => {
// const navigateTo = useNavigate(); const navigateTo = useNavigate();
const token = useDeviceToken(vapidKey); const token = useDeviceToken(vapidKey);
const [updateDeviceToken] = useUpdateDeviceTokenMutation(); const [updateDeviceToken] = useUpdateDeviceTokenMutation();
useEffect(() => { useEffect(() => {
@@ -24,19 +24,19 @@ const Notification = () => {
updateToken(token as string); updateToken(token as string);
}, [token]); }, [token]);
// useEffect(() => { useEffect(() => {
// const handleServiceWorkerMessage = (event: MessageEvent) => { const handleServiceWorkerMessage = (event: MessageEvent) => {
// const { newPath } = event.data; const { newPath } = event.data;
// if (newPath && document.hidden) { if (newPath && document.hidden) {
// navigateTo(newPath); navigateTo(newPath);
// } }
// }; };
// // https only // https only
// navigator.serviceWorker?.addEventListener("message", handleServiceWorkerMessage); navigator.serviceWorker?.addEventListener("message", handleServiceWorkerMessage);
// return () => { return () => {
// navigator.serviceWorker?.removeEventListener("message", handleServiceWorkerMessage); navigator.serviceWorker?.removeEventListener("message", handleServiceWorkerMessage);
// }; };
// }, []); }, []);
return null; return null;
}; };