Merge branch 'main' of http://45.93.137.91:3000/Rahaf/SweetHome
All checks were successful
Build frontend / build (push) Successful in 43s
All checks were successful
Build frontend / build (push) Successful in 43s
This commit is contained in:
@ -1,21 +1,29 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState, useRef } from "react";
|
||||||
import { requestNotificationPermission, onForegroundMessage } from "../utils/firebase";
|
import { requestNotificationPermission, onForegroundMessage } from "../utils/firebase";
|
||||||
import AuthService from "../services/AuthService";
|
import AuthService from "../services/AuthService";
|
||||||
|
|
||||||
export default function NotificationHandler() {
|
export default function NotificationHandler() {
|
||||||
const [notification, setNotification] = useState(null);
|
const [notification, setNotification] = useState(null);
|
||||||
const [unsubscribe, setUnsubscribe] = useState(null);
|
const initialized = useRef(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const user = AuthService.getUser();
|
// Wait for hydration to complete
|
||||||
if (!user) return;
|
const timer = setTimeout(() => {
|
||||||
|
if (initialized.current) return;
|
||||||
|
initialized.current = true;
|
||||||
|
|
||||||
// User is signed in — request permission and listen for messages
|
const token = AuthService.getToken();
|
||||||
requestNotificationPermission().then((token) => {
|
if (!token) {
|
||||||
if (token) {
|
console.log("[Notifications] Not signed in, skipping");
|
||||||
console.log("[Notifications] FCM token obtained");
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log("[Notifications] Signed in, requesting permission...");
|
||||||
|
requestNotificationPermission().then((fcmToken) => {
|
||||||
|
if (fcmToken) {
|
||||||
|
console.log("[Notifications] FCM token obtained:", fcmToken.substring(0, 20) + "...");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -26,8 +34,11 @@ export default function NotificationHandler() {
|
|||||||
setTimeout(() => setNotification(null), 5000);
|
setTimeout(() => setNotification(null), 5000);
|
||||||
});
|
});
|
||||||
|
|
||||||
setUnsubscribe(() => unsub);
|
// Cleanup on unmount
|
||||||
return () => unsub();
|
return () => unsub();
|
||||||
|
}, 1000); // Wait 1s for hydration
|
||||||
|
|
||||||
|
return () => clearTimeout(timer);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
if (!notification) return null;
|
if (!notification) return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user