1- // TODO: Identify how to create service worker with VITE properly
21// --- IMPORTANT: Service Worker for Background Messages ---
3- // Create a `firebase-messaging-sw.js` file in your public directory.
42// This file handles messages when your web app is not in the foreground.
53// It needs to import and initialize Firebase Messaging itself, using compat!
64
75// public/firebase-messaging-sw.js (using compat)
86importScripts ( 'https://www.gstatic.com/firebasejs/10.12.4/firebase-app-compat.js' ) ;
97importScripts ( 'https://www.gstatic.com/firebasejs/10.12.4/firebase-messaging-compat.js' ) ;
108
11- // Your Firebase configuration
9+ // Your Firebase configuration @see src/firebase.config.ts
1210const firebaseConfig = {
1311 apiKey : "AIzaSyDSTc5VVNNT32jRE4m8qr7hVbI8ahaIsRc" ,
1412 authDomain : "peaceinthemiddleeast.firebaseapp.com" ,
@@ -20,21 +18,68 @@ const firebaseConfig = {
2018 measurementId : "G-NKGPNTLDF1"
2119} ;
2220
23- // Initialize Firebase using the compat API
24- const firebaseApp = firebase . initializeApp ( firebaseConfig ) ; // Use firebase.initializeApp
21+ firebase . initializeApp ( firebaseConfig ) ;
2522
26- // Retrieve firebase messaging instance using the compat API
27- const messaging = firebaseApp . messaging ( ) ; // Use firebaseApp.messaging()
23+ let currentUserId = null ;
24+ let processedMoveIds = new Set ( ) ;
2825
29- // Handle incoming messages in the background (using compat messaging)
30- messaging . onBackgroundMessage ( ( payload ) => { // Use messaging.onBackgroundMessage
26+ console . log ( 'Service Worker: Loaded and Firebase initialized.' ) ;
27+
28+ firebase . messaging ( ) . onBackgroundMessage ( ( payload ) => {
3129 console . log ( 'Received background message ' , payload ) ;
3230
3331 const notificationTitle = payload . notification . title ;
3432 const notificationOptions = {
3533 body : payload . notification . body ,
36- icon : '/android-chrome-512x512.png' // Or your app icon
34+ icon : '/android-chrome-512x512.png'
3735 } ;
3836
3937 self . registration . showNotification ( notificationTitle , notificationOptions ) ;
4038} ) ;
39+
40+ self . addEventListener ( 'notificationclick' , event => {
41+ console . log ( 'Service Worker: Notification clicked.' , event . notification ) ;
42+ event . notification . close ( ) ;
43+
44+ const friend = event . notification . data . friend ; // This is opponent's UID, used for URL path
45+ // const gameId = event.notification.data.gameId; // Game's unique key, if needed for other logic
46+ // const moveId = event.notification.data.moveId; // Move was removed when notification was shown
47+
48+ if ( friend ) {
49+ // App URL structure is /:friend (e.g., /user123)
50+ const targetUrl = `${ self . location . origin } /${ friend } ` ;
51+ event . waitUntil (
52+ clients . matchAll ( { type : 'window' , includeUncontrolled : true } ) . then ( windowClients => {
53+ for ( let i = 0 ; i < windowClients . length ; i ++ ) {
54+ const client = windowClients [ i ] ;
55+ // Check if a window/tab with this game (friend) is already open.
56+ if ( ( client . url === targetUrl || client . url === `${ targetUrl } /` ) && 'focus' in client ) {
57+ console . log ( 'Service Worker: Focusing existing window for friend:' , friend ) ;
58+ return client . focus ( ) ;
59+ }
60+ }
61+ if ( clients . openWindow ) {
62+ console . log ( 'Service Worker: Opening new window for friend:' , friend ) ;
63+ return clients . openWindow ( targetUrl ) ;
64+ }
65+ } )
66+ ) ;
67+ } else {
68+ console . log ( 'Service Worker: No friend in notification data, opening main page.' ) ;
69+ if ( clients . openWindow ) { // Fallback to open the main page
70+ return clients . openWindow ( '/' ) ;
71+ }
72+ }
73+ } ) ;
74+
75+ self . addEventListener ( 'activate' , event => {
76+ console . log ( 'Service Worker: Activating...' ) ;
77+ event . waitUntil (
78+ self . clients . claim ( ) . then ( ( ) => { // Take control of all clients immediately
79+ console . log ( 'Service Worker: Claimed clients.' ) ;
80+ // On activation, client should send 'SET_USER_ID' if user is logged in.
81+ // If SW persisted userId (e.g. in IndexedDB), it could be read here to setup listener early.
82+ // Example: restoreUserIdAndSetupListener();
83+ } )
84+ ) ;
85+ } ) ;
0 commit comments