|
| 1 | +// client/src/firebase-messaging-setup.ts (using compat) |
| 2 | + |
| 3 | +// Import the compat versions of the SDKs you need |
| 4 | +import firebase from 'firebase/compat/app'; |
| 5 | +import 'firebase/compat/messaging'; |
| 6 | +import 'firebase/compat/database'; |
| 7 | +import 'firebase/compat/auth'; |
| 8 | + |
| 9 | +// Your Firebase configuration (get this from your Firebase project settings) |
| 10 | +const firebaseConfig = { |
| 11 | + // ... your config |
| 12 | +}; |
| 13 | + |
| 14 | +// Initialize Firebase (using the compat app import) |
| 15 | +const firebaseApp = firebase.initializeApp(firebaseConfig); |
| 16 | +// Access services directly from the app instance or the global firebase object |
| 17 | +const messaging = firebaseApp.messaging(); // Or firebase.messaging() if using script tags |
| 18 | +const db = firebaseApp.database(); // Or firebase.database() |
| 19 | +const auth = firebaseApp.auth(); // Or firebase.auth() |
| 20 | + |
| 21 | + |
| 22 | +// Get the FCM registration token and save it to the database |
| 23 | +async function saveMessagingDeviceToken() { |
| 24 | + const userId = auth.currentUser?.uid; |
| 25 | + if (!userId) { |
| 26 | + console.log('User not logged in, cannot save token.'); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + try { |
| 31 | + // Request permission (using the compat messaging instance) |
| 32 | + const permission = await Notification.requestPermission(); |
| 33 | + if (permission === 'granted') { |
| 34 | + console.log('Notification permission granted.'); |
| 35 | + |
| 36 | + // Get token (using the compat messaging instance) |
| 37 | + // VAPID key is required for web push (generate in Firebase Console -> Project settings -> Cloud Messaging) |
| 38 | + // Note: getToken is part of the v9 web API, which compat wraps |
| 39 | + const currentToken = await messaging.getToken({ vapidKey: 'YOUR_VAPID_KEY_HERE' }); |
| 40 | + |
| 41 | + if (currentToken) { |
| 42 | + console.log('FCM registration token:', currentToken); |
| 43 | + // Save the token to your database (using compat database syntax) |
| 44 | + await db.ref(`/users/${userId}/fcmToken`).set(currentToken); |
| 45 | + console.log('FCM token saved to database.'); |
| 46 | + } else { |
| 47 | + console.log('No registration token available. Request permission to generate one.'); |
| 48 | + } |
| 49 | + } else { |
| 50 | + console.log('Unable to get notification permission.'); |
| 51 | + } |
| 52 | + } catch (error) { |
| 53 | + console.error('An error occurred while retrieving token or setting it up.', error); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Listen for incoming messages while the app is in the foreground (using compat messaging) |
| 58 | +// Note: onMessage is part of the v9 web API, which compat wraps |
| 59 | +messaging.onMessage((payload) => { |
| 60 | + console.log('Message received in foreground.', payload); |
| 61 | + // Display the notification to the user in the app UI |
| 62 | + if (payload.notification) { |
| 63 | + alert(`New move in game: ${payload.notification.body}`); // Simple example |
| 64 | + } |
| 65 | +}); |
| 66 | + |
| 67 | +// see public/firebase-messaging-sw.js |
| 68 | + |
| 69 | +// Call this function when the user logs in or perhaps when the app loads if they are already logged in |
| 70 | +auth.onAuthStateChanged((user) => { // Using compat auth instance |
| 71 | + if (user) { |
| 72 | + saveMessagingDeviceToken(); |
| 73 | + } else { |
| 74 | + // User is signed out |
| 75 | + console.log('User signed out, token not needed or remove token from DB'); |
| 76 | + // Optional: remove the token from the database if the user signs out |
| 77 | + } |
| 78 | +}); |
0 commit comments