aboutsummaryrefslogtreecommitdiffstats
path: root/site/src/firebase.ts
blob: 2cf4c8f26439cdce33f3eebc02f38436a0c8e3b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { initializeApp } from "firebase/app";
import { getMessaging, Messaging, onMessage } from "firebase/messaging";

const firebaseConfig = {
  apiKey: import.meta.env.VITE_FIREBASE_API_KEY,
  authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN,
  projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID,
  storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER_ID,
  appId: import.meta.env.VITE_FIREBASE_APP_ID,
};

const app = initializeApp(firebaseConfig);

export const messaging: Messaging = getMessaging(app);
let foregroundInitialized = false;

// Handle foreground messages
export const initializeForegroundNotifications = () => {
  if (foregroundInitialized) return; // Prevent double registration
  foregroundInitialized = true;

  onMessage(messaging, (payload) => {
    console.log("[firebase.ts] Foreground message received", payload);

    if (Notification.permission !== "granted") return;

    const data = payload.data || {};
    const title = data.title || "New Update";
    const options: NotificationOptions = {
      body: data.body || "You have a new notification",
      icon: data.icon || "/android/android-launchericon-192-192.png",
      badge: data.badge || "/android/android-launchericon-72-72.png",
      tag: data.tag || "default-tag",
      requireInteraction: data.requireInteraction === "true",
      silent: false,
      data,
    };

    const notification = new Notification(title, options);

    notification.onclick = (event) => {
      event.preventDefault();
      notification.close();
      const url = data.url || "/";
      window.open(url, "_blank");
    };

    if (data.requireInteraction !== "true") {
      setTimeout(() => notification.close(), 10000);
    }
  });

  console.log("[firebase.ts] Foreground notification handler initialized");
};
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage