forked from amer2004/SweetHome
removing console.log
This commit is contained in:
@ -12,16 +12,15 @@ const firebaseConfig = {
|
||||
};
|
||||
|
||||
// Initialize Firebase (avoid duplicate init in SSR)
|
||||
const app = getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0];
|
||||
const app =
|
||||
getApps().length === 0 ? initializeApp(firebaseConfig) : getApps()[0];
|
||||
|
||||
// Get messaging instance (only works in browser)
|
||||
let messaging = null;
|
||||
if (typeof window !== "undefined" && "serviceWorker" in navigator) {
|
||||
try {
|
||||
messaging = getMessaging(app);
|
||||
} catch (e) {
|
||||
console.warn("[Firebase] Messaging init failed:", e.message);
|
||||
}
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
// Request notification permission and get FCM token
|
||||
@ -31,43 +30,46 @@ export async function requestNotificationPermission() {
|
||||
try {
|
||||
const permission = await Notification.requestPermission();
|
||||
if (permission !== "granted") {
|
||||
console.log("[FCM] Notification permission denied");
|
||||
return null;
|
||||
}
|
||||
|
||||
const registration = await navigator.serviceWorker.register("/firebase-messaging-sw.js");
|
||||
const registration = await navigator.serviceWorker.register(
|
||||
"/firebase-messaging-sw.js"
|
||||
);
|
||||
|
||||
const token = await getToken(messaging, {
|
||||
vapidKey: "BGZ4Fo8rRhoTdStLGlCySDZOnAX4ekCA0e3HDWXL5uEi2kOnXynYjbaDbY15002phUrFqxBpPPFHgfH2VhrmFDU",
|
||||
vapidKey:
|
||||
"BGZ4Fo8rRhoTdStLGlCySDZOnAX4ekCA0e3HDWXL5uEi2kOnXynYjbaDbY15002phUrFqxBpPPFHgfH2VhrmFDU",
|
||||
serviceWorkerRegistration: registration,
|
||||
});
|
||||
|
||||
console.log("[FCM] Token:", token);
|
||||
|
||||
// Send token to backend
|
||||
if (token) {
|
||||
try {
|
||||
const authToken = localStorage.getItem("auth_token");
|
||||
|
||||
if (authToken) {
|
||||
const apiBase = process.env.NEXT_PUBLIC_API_URL || "https://45.93.137.91.nip.io/api";
|
||||
const apiBase =
|
||||
process.env.NEXT_PUBLIC_API_URL ||
|
||||
"https://45.93.137.91.nip.io/api";
|
||||
|
||||
await fetch(`${apiBase}/User/SetFCMToken`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${authToken}`,
|
||||
},
|
||||
body: JSON.stringify({ token, deviceType: 2 }), // 2 = Web
|
||||
body: JSON.stringify({
|
||||
token,
|
||||
deviceType: 2,
|
||||
}),
|
||||
});
|
||||
console.log("[FCM] Token sent to backend");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("[FCM] Failed to send token to backend:", err);
|
||||
}
|
||||
} catch (err) {}
|
||||
}
|
||||
|
||||
return token;
|
||||
} catch (err) {
|
||||
console.error("[FCM] Error getting token:", err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@ -77,9 +79,8 @@ export function onForegroundMessage(callback) {
|
||||
if (!messaging) return () => {};
|
||||
|
||||
return onMessage(messaging, (payload) => {
|
||||
console.log("[FCM] Foreground message:", payload);
|
||||
callback(payload);
|
||||
});
|
||||
}
|
||||
|
||||
export { app, messaging };
|
||||
export { app, messaging };
|
||||
Reference in New Issue
Block a user