From df9711f53916cf4dc4828b6707d4d239d8f0354c Mon Sep 17 00:00:00 2001 From: Claw AI Date: Tue, 31 Mar 2026 19:52:47 +0000 Subject: [PATCH] fix: only request notification permissions for signed-in users --- app/components/NotificationHandler.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/components/NotificationHandler.js b/app/components/NotificationHandler.js index 9e34f69..9afd2e3 100644 --- a/app/components/NotificationHandler.js +++ b/app/components/NotificationHandler.js @@ -2,31 +2,32 @@ import { useEffect, useState } from "react"; import { requestNotificationPermission, onForegroundMessage } from "../utils/firebase"; +import AuthService from "../services/AuthService"; export default function NotificationHandler() { const [notification, setNotification] = useState(null); + const [unsubscribe, setUnsubscribe] = useState(null); useEffect(() => { - // Request permission and get token + const user = AuthService.getUser(); + if (!user) return; + + // User is signed in — request permission and listen for messages requestNotificationPermission().then((token) => { if (token) { console.log("[Notifications] FCM token obtained"); - // TODO: Send token to your backend to register the device - // e.g. apiFetch('/Notifications/RegisterDevice', { method: 'POST', body: { token } }) } }); - // Listen for foreground messages - const unsubscribe = onForegroundMessage((payload) => { + const unsub = onForegroundMessage((payload) => { const title = payload.notification?.title || payload.data?.title || "Sweet Home"; const body = payload.notification?.body || payload.data?.body || ""; setNotification({ title, body }); - - // Auto-dismiss after 5 seconds setTimeout(() => setNotification(null), 5000); }); - return () => unsubscribe(); + setUnsubscribe(() => unsub); + return () => unsub(); }, []); if (!notification) return null;