55 useRouter ,
66 useRouterState ,
77} from "@tanstack/react-router" ;
8+ import { initializeApp } from "firebase/app" ;
9+ import { getMessaging , getToken , onMessage } from "firebase/messaging" ;
810import { useAtomValue , useSetAtom } from "jotai" ;
911import { Suspense , useEffect } from "react" ;
1012import { AppBar } from "../components/AppBar" ;
@@ -13,9 +15,13 @@ import { DevBanner } from "../components/DevBanner";
1315import { InstallBanner } from "../components/InstallBanner" ;
1416import { SideMenu } from "../components/menu/SideMenu" ;
1517import { PwaReloadBanner } from "../components/PwaReloadBanner" ;
18+
1619import { onboardedAtom } from "../onboarding" ;
1720import { pageTitleAtom } from "../pageState" ;
1821
22+ const FIREBASE_CONFIG = JSON . parse ( import . meta. env . VITE_FIREBASE_CONFIG ) ;
23+ const FIREBASE_VAPID_KEY = import . meta. env . VITE_FIREBASE_VAPID_KEY ;
24+
1925export const Route = createFileRoute ( "/_app" ) ( {
2026 component : RouteComponent ,
2127} ) ;
@@ -51,6 +57,55 @@ function RouteComponent() {
5157 } ,
5258 } ) ;
5359
60+ useEffect ( ( ) => {
61+ navigator . serviceWorker . ready . then ( ( registration ) => {
62+ const firebaseApp = initializeApp ( FIREBASE_CONFIG ) ;
63+ const messaging = getMessaging ( firebaseApp ) ;
64+
65+ getToken ( messaging , {
66+ vapidKey : FIREBASE_VAPID_KEY ,
67+ serviceWorkerRegistration : registration ,
68+ } )
69+ . then ( ( token ) => {
70+ console . log ( "FCM token:" , token ) ;
71+
72+ onMessage ( messaging , ( payload ) => {
73+ console . log ( "Foreground message received:" , payload ) ;
74+
75+ registration . showNotification (
76+ payload . notification ?. title || "Notification" ,
77+ {
78+ body : payload . notification ?. body ,
79+ } ,
80+ ) ;
81+ } ) ;
82+
83+ fetch ( "/notifications/api/tenants/jamboree26/register" , {
84+ method : "POST" ,
85+ headers : {
86+ "Content-Type" : "application/json" ,
87+ } ,
88+ body : JSON . stringify ( { tokens : [ token ] } ) ,
89+ } )
90+ . then ( ( res ) => {
91+ if ( ! res . ok ) {
92+ throw new Error (
93+ `Failed to register FCM token: ${ res . statusText } ` ,
94+ ) ;
95+ }
96+
97+ console . log ( "FCM token registered with backend" ) ;
98+ } )
99+ . catch ( ( e ) => {
100+ console . error ( "Failed to register FCM token with backend:" , e ) ;
101+ } ) ;
102+ } )
103+ . catch ( ( e ) => {
104+ console . error ( "Failed to get FCM token:" , e ) ;
105+ } ) ;
106+ } ) ;
107+ } , [ ] ) ;
108+
54109 // biome-ignore lint/correctness/useExhaustiveDependencies: We only want this to run on load
55110 useEffect ( ( ) => {
56111 if ( ! onboarded ) {
0 commit comments