From 1a36c9aad08f04bcfc8305220ccdc9df0c8307d0 Mon Sep 17 00:00:00 2001 From: Pavel Meyer Date: Thu, 5 Sep 2024 19:34:55 +0300 Subject: [PATCH] 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]);