"use client"; 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(() => { 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"); } }); 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 }); setTimeout(() => setNotification(null), 5000); }); setUnsubscribe(() => unsub); return () => unsub(); }, []); if (!notification) return null; return (
{notification.title}
{notification.body}