"use client"; import { useEffect, useState } from "react"; import { requestNotificationPermission, onForegroundMessage } from "../utils/firebase"; export default function NotificationHandler() { const [notification, setNotification] = useState(null); useEffect(() => { // Request permission and get token 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 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(); }, []); if (!notification) return null; return (
{notification.title}
{notification.body}