Skip to content

Commit 2d929fa

Browse files
committed
feat: just got notifications to work
1 parent ac57a56 commit 2d929fa

5 files changed

Lines changed: 93 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ dev-dist
2929
.env
3030
.env.*
3131
!.env.example
32+
33+
.j26

.j26.local.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
services:
2+
auth:
3+
mode: docker
4+
env:
5+
PUBLIC_URL: https://local.j26.se/auth
6+
ALLOWED_REDIRECT_DOMAINS: local.j26.se
7+
OIDC_SERVER: https://dev.id.scouterna.se/realms/jamboree26
8+
OIDC_CLIENT_ID: j26-auth-local
9+
OIDC_CLIENT_SECRET: nFpewcBpLbdwSdcg42zpEDu6omS31u03
10+
app:
11+
mode: local
12+
hostPort: 5173

src/routes/_app.tsx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
useRouter,
66
useRouterState,
77
} from "@tanstack/react-router";
8+
import { initializeApp } from "firebase/app";
9+
import { getMessaging, getToken, onMessage } from "firebase/messaging";
810
import { useAtomValue, useSetAtom } from "jotai";
911
import { Suspense, useEffect } from "react";
1012
import { AppBar } from "../components/AppBar";
@@ -13,9 +15,13 @@ import { DevBanner } from "../components/DevBanner";
1315
import { InstallBanner } from "../components/InstallBanner";
1416
import { SideMenu } from "../components/menu/SideMenu";
1517
import { PwaReloadBanner } from "../components/PwaReloadBanner";
18+
1619
import { onboardedAtom } from "../onboarding";
1720
import { 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+
1925
export 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) {

src/sw/sw.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { initializeApp } from "firebase/app";
2+
import { getMessaging, onBackgroundMessage } from "firebase/messaging/sw";
13
import { CacheableResponsePlugin } from "workbox-cacheable-response";
24
import { cleanupOutdatedCaches, precacheAndRoute } from "workbox-precaching";
35
import { Route, registerRoute } from "workbox-routing";
@@ -10,9 +12,28 @@ import {
1012
declare let self: ServiceWorkerGlobalScope;
1113

1214
import { ExpirationPlugin } from "workbox-expiration";
13-
1415
import { osmTilesRoute } from "./sw-osm-tiles";
1516

17+
const FIREBASE_CONFIG = JSON.parse(import.meta.env.VITE_FIREBASE_CONFIG);
18+
19+
const firebaseApp = initializeApp(FIREBASE_CONFIG);
20+
const messaging = getMessaging(firebaseApp);
21+
22+
onBackgroundMessage(messaging, (payload) => {
23+
console.log("Background message received:", payload);
24+
25+
self.registration
26+
.showNotification(payload.notification?.title || "Notification", {
27+
body: payload.notification?.body,
28+
})
29+
.then(() => {
30+
console.log("Notification shown successfully");
31+
})
32+
.catch((e) => {
33+
console.error("Failed to show notification:", e);
34+
});
35+
});
36+
1637
cleanupOutdatedCaches();
1738

1839
precacheAndRoute(self.__WB_MANIFEST);

vite.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export default defineConfig(() => {
6161
injectRegister: "script",
6262
strategies: "injectManifest",
6363
devOptions: {
64-
// enabled: true,
64+
enabled: true,
65+
type: "module",
6566
},
6667
srcDir: "src/sw",
6768
filename: "sw.ts",

0 commit comments

Comments
 (0)