Website : rimsha.abasa.com
backdoor
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
mudeer-web-backup30April26
/
src
/
lib
/
Filename :
firebase.ts
back
Copy
import { initializeApp } from "firebase/app"; import { getAnalytics } from "firebase/analytics"; import { getMessaging, getToken, onMessage } from "firebase/messaging"; const firebaseConfig = { apiKey: "AIzaSyCbIvnNxswOK6arm8KtSgItNloq-SuHq5w", authDomain: "webnexus-proj.firebaseapp.com", projectId: "webnexus-proj", storageBucket: "webnexus-proj.firebasestorage.app", messagingSenderId: "272445720449", appId: "1:272445720449:web:9df3873d05069c65f68a80", measurementId: "G-ZG6XN7KYRH" }; // Initialize Firebase const app = initializeApp(firebaseConfig); const analytics = typeof window !== 'undefined' ? getAnalytics(app) : null; const messaging = typeof window !== 'undefined' ? getMessaging(app) : null; const VAPID_KEY = "BFy-DWcLuw2VlVJy0M09-Ju5qxNmeo1N15FEaungpZbtdDQe4P6TBJaFh-GGXjVlY6CQPP50IETCPKWcg8bHAo8"; export { app, analytics, messaging, VAPID_KEY }; // FCM Token management functions export const requestNotificationPermission = async (): Promise<string | null> => { try { if (!messaging) { // console.error('❌ Firebase messaging not initialized'); return null; } // Check current permission status const currentPermission = Notification.permission; // console.log('🔍 Current notification permission:', currentPermission); if (currentPermission === 'denied') { // console.log('🚫 Notifications are blocked. User needs to manually enable them in browser settings.'); return null; } let permission = currentPermission; // Only request permission if it's not already granted if (currentPermission === 'default') { // console.log('📝 Requesting notification permission...'); permission = await Notification.requestPermission(); // console.log('📝 Permission result:', permission); } if (permission === 'granted') { // console.log('✅ Permission granted, requesting FCM token...'); // Add more debugging for the getToken call try { const token = await getToken(messaging, { vapidKey: VAPID_KEY }); if (token) { // console.log('🎉 FCM Token generated successfully:', token.substring(0, 20) + '...'); return token; } else { // console.error('❌ getToken returned null - this usually means:'); // console.error(' 1. Service worker is not registered properly'); // console.error(' 2. VAPID key is invalid'); // console.error(' 3. Firebase config is incorrect'); return null; } } catch (tokenError) { // console.error('💥 Error getting FCM token:', tokenError); console.error('Token error details:', { name: tokenError.name, message: tokenError.message, code: tokenError.code }); return null; } } else if (permission === 'denied') { // console.log('❌ Notification permission denied by user'); return null; } else { // console.log('⚠️ Notification permission dismissed (default)'); return null; } } catch (error) { // console.error('💥 Error in requestNotificationPermission:', error); console.error('Error details:', { name: error.name, message: error.message, stack: error.stack }); return null; } }; // Add function to check if service worker is registered export const checkServiceWorkerStatus = () => { if ('serviceWorker' in navigator) { navigator.serviceWorker.getRegistrations().then(registrations => { // console.log('🔧 Service Worker registrations:', registrations.length); // registrations.forEach((registration, index) => { // console.log(` ${index + 1}. ${registration.scope}`); // console.log(` Active: ${registration.active?.scriptURL || 'none'}`); // }); }); } else { // console.log('❌ Service Worker not supported'); } }; // Add function to validate Firebase config export const validateFirebaseConfig = () => { // console.log('🔧 Firebase Config Check:'); // console.log(' App initialized:', !!app); // console.log(' Messaging initialized:', !!messaging); // console.log(' VAPID key present:', !!VAPID_KEY); // console.log(' VAPID key length:', VAPID_KEY.length); // console.log(' Project ID:', firebaseConfig.projectId); // console.log(' App ID:', firebaseConfig.appId); }; // Add a function to check permission status export const getNotificationPermissionStatus = (): NotificationPermission => { return Notification.permission; }; // Add a function to show user-friendly permission instructions export const showPermissionInstructions = () => { const permission = getNotificationPermissionStatus(); if (permission === 'denied') { return { blocked: true, message: 'Notifications are blocked. To enable them:\n1. Click the lock icon next to the URL\n2. Change Notifications from "Block" to "Allow"\n3. Refresh the page', canRetry: false }; } else if (permission === 'default') { return { blocked: false, message: 'Click "Allow" when prompted to enable notifications', canRetry: true }; } else { return { blocked: false, message: 'Notifications are enabled', canRetry: false }; } }; export const setupFCMListener = (callback: (payload: any) => void): (() => void) | null => { if (!messaging) { console.warn('Messaging not available'); return null; } // This creates a CONTINUOUS listener return onMessage(messaging, callback); };