fix: only request notification permissions for signed-in users
All checks were successful
Build frontend / build (push) Successful in 39s

This commit is contained in:
Claw AI
2026-03-31 19:52:47 +00:00
parent 2bea2d190c
commit df9711f539

View File

@ -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;