From 1a36c9aad08f04bcfc8305220ccdc9df0c8307d0 Mon Sep 17 00:00:00 2001 From: Pavel Meyer Date: Thu, 5 Sep 2024 19:34:55 +0300 Subject: [PATCH 1/4] CW-service-worker-performance Added check for registered service-workers --- .../NotificationsHandler.tsx | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/src/pages/App/handlers/NotificationsHandler/NotificationsHandler.tsx b/src/pages/App/handlers/NotificationsHandler/NotificationsHandler.tsx index de144355b..44f26f4f5 100644 --- a/src/pages/App/handlers/NotificationsHandler/NotificationsHandler.tsx +++ b/src/pages/App/handlers/NotificationsHandler/NotificationsHandler.tsx @@ -8,37 +8,60 @@ const NotificationsHandler: FC = () => { const userId = user?.uid; const [isRegistered, setIsRegistered] = useState(false); + function initServiceWorker() { + navigator.serviceWorker + .register("/firebase-messaging-sw.js") + .then((registration) => { + setIsRegistered(true); + return registration; + }) + .catch((err) => { + console.log("ServiceWorker registration failed: ", err); + }); + } + + // Check if the service worker is already registered or register a new one useEffect(() => { if ("serviceWorker" in navigator) { navigator.serviceWorker - .register("/firebase-messaging-sw.js") - .then((registration) => { - setIsRegistered(true); - return registration; + .getRegistration("/firebase-messaging-sw.js") + .then((existingRegistration) => { + if (existingRegistration) { + setIsRegistered(true); + } else { + initServiceWorker(); + } + + return; }) .catch((err) => { - console.log("ServiceWorker registration failed: ", err); + console.log("Error checking service worker registration: ", err); }); } }, []); + // Handle notification permissions and foreground message listener useEffect(() => { - if (!userId && !isRegistered) { + if (!userId || !isRegistered) { return; } let unsubscribeOnMessage; (async () => { const hasPermissions = await NotificationService.requestPermissions(); - if (hasPermissions) { - await NotificationService.saveFCMToken(); - - unsubscribeOnMessage = NotificationService.onForegroundMessage(); + if (!hasPermissions) { + console.log("Notification permissions denied"); + return; } + + await NotificationService.saveFCMToken(); + unsubscribeOnMessage = NotificationService.onForegroundMessage(); })(); return () => { - unsubscribeOnMessage && unsubscribeOnMessage(); + if (unsubscribeOnMessage) { + unsubscribeOnMessage(); + } }; }, [userId, isRegistered]); From 11771614d36ca2f532c838b134bccb2c6afdcceb Mon Sep 17 00:00:00 2001 From: Pavel Meyer Date: Tue, 10 Sep 2024 11:49:37 +0300 Subject: [PATCH 2/4] CW-service-worker-performance Added firefox permissions --- src/shared/components/Header/Header.tsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/shared/components/Header/Header.tsx b/src/shared/components/Header/Header.tsx index 672bd3bff..8d10f043e 100755 --- a/src/shared/components/Header/Header.tsx +++ b/src/shared/components/Header/Header.tsx @@ -3,6 +3,7 @@ import { useDispatch, useSelector } from "react-redux"; import { Link, RouteProps, useHistory } from "react-router-dom"; import classNames from "classnames"; import { Routes } from "@/pages/MyAccount/components/Routes"; +import { NotificationService } from "@/services"; import { Loader } from "@/shared/components"; import { useAnyMandatoryRoles, @@ -82,7 +83,8 @@ const Header = () => { setShowAccountLinks(isMyAccountRoute); }, [showMenu, isMyAccountRoute]); - const handleLogIn = useCallback(() => { + const handleLogIn = useCallback(async () => { + await NotificationService.requestPermissions(); dispatch(setLoginModalState({ isShowing: true })); setShowMenu(false); }, [dispatch]); @@ -116,7 +118,8 @@ const Header = () => { dispatch(logOut()); }; - const handleLaunchApp = () => { + const handleLaunchApp = async () => { + await NotificationService.requestPermissions(); history.push(ROUTE_PATHS.INBOX); }; From 956b5432e43a22edad105b306a7d9fdae6b1e9b1 Mon Sep 17 00:00:00 2001 From: Pavel Meyer Date: Tue, 10 Sep 2024 13:35:47 +0300 Subject: [PATCH 3/4] CW-service-worker-performance Fixed settings for firestore --- src/services/Notification.ts | 1 + src/shared/utils/firebase.tsx | 1 + 2 files changed, 2 insertions(+) diff --git a/src/services/Notification.ts b/src/services/Notification.ts index 25e7597b3..1ccd7d342 100644 --- a/src/services/Notification.ts +++ b/src/services/Notification.ts @@ -41,6 +41,7 @@ class NotificationService { const token = await firebase.messaging().getToken({ vapidKey: firebaseConfig.vapidKey }); if (token) { + console.log('---token',token); await Api.post( this.endpoints.setFCMToken, { diff --git a/src/shared/utils/firebase.tsx b/src/shared/utils/firebase.tsx index 80147a0c8..675511068 100644 --- a/src/shared/utils/firebase.tsx +++ b/src/shared/utils/firebase.tsx @@ -30,6 +30,7 @@ function handlePersistenceError(err: any) { clearFirestoreCache(); } else { console.error("Error enabling persistence:", err); + reinitializeFirestoreWithPersistence(); } } From dc5b99a8df066d09af6b120ddd6159e53fd0931e Mon Sep 17 00:00:00 2001 From: Pavel Meyer Date: Tue, 10 Sep 2024 14:30:32 +0300 Subject: [PATCH 4/4] CW-service-worker-performance Remove console.log --- src/services/Notification.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/services/Notification.ts b/src/services/Notification.ts index 1ccd7d342..25e7597b3 100644 --- a/src/services/Notification.ts +++ b/src/services/Notification.ts @@ -41,7 +41,6 @@ class NotificationService { const token = await firebase.messaging().getToken({ vapidKey: firebaseConfig.vapidKey }); if (token) { - console.log('---token',token); await Api.post( this.endpoints.setFCMToken, {