From 98ead632b7715b8f1768c962a37b9efa0a684484 Mon Sep 17 00:00:00 2001
From: Pinapelz <yukais@pinapelz.com>
Date: Fri, 3 Oct 2025 02:57:04 -0700
Subject: fix: double notification bug

---
 site/src/components/NotificationButton.tsx | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

(limited to 'site/src/components/NotificationButton.tsx')

diff --git a/site/src/components/NotificationButton.tsx b/site/src/components/NotificationButton.tsx
index 8f4fb61..66109a3 100644
--- a/site/src/components/NotificationButton.tsx
+++ b/site/src/components/NotificationButton.tsx
@@ -25,7 +25,7 @@ export default function NotificationButton({ className = "", isMoe = false }: No
       if ('serviceWorker' in navigator) {
         const registration = await navigator.serviceWorker.getRegistration('/firebase-messaging-sw.js');
         setIsRegistered(!!registration);
-        
+
         // Initialize foreground notifications if already registered
         if (registration && Notification.permission === "granted") {
           initializeForegroundNotifications();
@@ -45,15 +45,23 @@ export default function NotificationButton({ className = "", isMoe = false }: No
       setPermission(permissionResult);
 
       if (permissionResult === "granted") {
-        // Register service worker
-        const registration = await navigator.serviceWorker.register('/firebase-messaging-sw.js');
-        console.log("Service Worker registered:", registration);
-        const token = await getToken(messaging, { vapidKey: VAPID_KEY });
+        let registration = await navigator.serviceWorker.getRegistration('/firebase-messaging-sw.js');
+        if (!registration) {
+          registration = await navigator.serviceWorker.register('/firebase-messaging-sw.js');
+          console.log("Service Worker registered:", registration);
+        } else {
+          console.log("Reusing existing Service Worker:", registration);
+        }
+
+        const token = await getToken(messaging, {
+          vapidKey: VAPID_KEY,
+          serviceWorkerRegistration: registration,
+        });
         console.log("FCM Token:", token);
-        // Store token locally (you might want to send this to your server)
+
         localStorage.setItem('fcm_token', token);
 
-        // Initialize foreground notification handler
+        // Foreground notification listener
         initializeForegroundNotifications();
 
         setIsRegistered(true);
@@ -76,6 +84,7 @@ export default function NotificationButton({ className = "", isMoe = false }: No
       await deleteToken(messaging);
       console.log("FCM token deleted");
       localStorage.removeItem('fcm_token');
+
       if ('serviceWorker' in navigator) {
         const registration = await navigator.serviceWorker.getRegistration('/firebase-messaging-sw.js');
         if (registration) {
@@ -93,7 +102,7 @@ export default function NotificationButton({ className = "", isMoe = false }: No
     }
   };
 
-  // Determine button state and action
+  // Determine button content
   const getButtonContent = () => {
     if (loading) {
       return (
@@ -141,7 +150,6 @@ export default function NotificationButton({ className = "", isMoe = false }: No
 
   const handleClick = () => {
     if (permission === "denied") {
-      // Can't re-request permission if denied
       alert("Notifications are blocked. Please enable them in your browser settings.");
       return;
     }
-- 
cgit v1.2.3

