From b4633dbaaebcec7cae0cea8071dc152496cf97e8 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Fri, 19 Jul 2024 14:32:20 -0600 Subject: [PATCH] Add notification config --- frigate/app.py | 4 ++-- frigate/comms/firebase.py | 4 ++-- frigate/config.py | 6 ++++++ web/public/firebase-messaging-sw.js | 16 ++++++---------- web/src/hooks/use-firebase.ts | 23 +++++++++++++++++++---- web/src/types/notifications.ts | 9 --------- 6 files changed, 35 insertions(+), 27 deletions(-) delete mode 100644 web/src/types/notifications.ts diff --git a/frigate/app.py b/frigate/app.py index 063d2471a..a0782c850 100644 --- a/frigate/app.py +++ b/frigate/app.py @@ -402,8 +402,8 @@ class FrigateApp: if self.config.mqtt.enabled: comms.append(MqttClient(self.config)) - # TODO check if notifications are enabled - comms.append(FirebaseClient(self.config, self.stop_event)) + if self.config.notifications.enabled: + comms.append(FirebaseClient(self.config, self.stop_event)) comms.append(WebSocketClient(self.config)) comms.append(self.inter_process_communicator) diff --git a/frigate/comms/firebase.py b/frigate/comms/firebase.py index fe0c05888..5934d1c2c 100644 --- a/frigate/comms/firebase.py +++ b/frigate/comms/firebase.py @@ -75,10 +75,10 @@ class FirebaseMessenger(threading.Thread): ), webpush=messaging.WebpushConfig( fcm_options=messaging.WebpushFCMOptions( - link=f"https://localhost:5173/review?id={reviewId}" + link=f"{self.config.notifications.base_url}/review?id={reviewId}" ) ), - data={"id": reviewId, "imageUrl": f'https://localhost:5173{payload["after"]["thumb_path"].replace("/media/frigate", "")}'}, + data={"id": reviewId, "imageUrl": f'{self.config.notifications.base_url}{payload["after"]["thumb_path"].replace("/media/frigate", "")}'}, tokens=[ "cNNicZp6S92qn4kAVJnzd7:APA91bGv-MvDmNoZ2xqJTkPyCTmyv2WG0tfwIqWUuNtq3SXlpQJpdPCCjTEehOLDa0Yphv__KdxOQYEfaFvYfTW2qQevX-tSnRCVa_sJazQ_rfTervpo_zBVJD1T5GfYaY6kr41Wr_fP" ], diff --git a/frigate/config.py b/frigate/config.py index 20a540919..114ce5a6d 100644 --- a/frigate/config.py +++ b/frigate/config.py @@ -169,6 +169,11 @@ class AuthConfig(FrigateBaseModel): hash_iterations: int = Field(default=600000, title="Password hash iterations") +class NotificationConfig(FrigateBaseModel): + enabled: bool = Field(default=False, title="Enable notifications") + base_url: Optional[str] = Field(default=None, title="Base url for notification link and image.") + + class StatsConfig(FrigateBaseModel): amd_gpu_stats: bool = Field(default=True, title="Enable AMD GPU stats.") intel_gpu_stats: bool = Field(default=True, title="Enable Intel GPU stats.") @@ -1361,6 +1366,7 @@ class FrigateConfig(FrigateBaseModel): default_factory=dict, title="Frigate environment variables." ) ui: UIConfig = Field(default_factory=UIConfig, title="UI configuration.") + notifications: NotificationConfig = Field(default_factory=NotificationConfig, title="Notification Config") telemetry: TelemetryConfig = Field( default_factory=TelemetryConfig, title="Telemetry configuration." ) diff --git a/web/public/firebase-messaging-sw.js b/web/public/firebase-messaging-sw.js index a788a3574..f83a93227 100644 --- a/web/public/firebase-messaging-sw.js +++ b/web/public/firebase-messaging-sw.js @@ -7,15 +7,11 @@ importScripts("https://www.gstatic.com/firebasejs/8.10.1/firebase-messaging.js") // Initialize the Firebase app in the service worker by passing in // your app's Firebase config object. // https://firebase.google.com/docs/web/setup#config-object -firebase.initializeApp({ - apiKey: "AIzaSyCoweRLtvai8iNwhsoT-GH_CH_0pckqMmA", - authDomain: "frigate-ed674.firebaseapp.com", - projectId: "frigate-ed674", - storageBucket: "frigate-ed674.appspot.com", - messagingSenderId: "76314288339", - appId: "1:76314288339:web:090e170610d3bf0966f426", - measurementId: "G-GZ1JKNDJZK", -}); +const fbConfig = await ( + await fetch(`${window.location.href}/firebase-config.json`) +).json(); + +firebase.initializeApp(fbConfig); // Retrieve an instance of Firebase Messaging so that it can handle background // messages. @@ -29,7 +25,7 @@ messaging.onBackgroundMessage((payload) => { // Customize notification here const notificationOptions = { body: payload.notification.body, - icon: , + icon: payload.data.imageUrl, tag: payload.data.id, // ensure that the notifications for same items are written over }; diff --git a/web/src/hooks/use-firebase.ts b/web/src/hooks/use-firebase.ts index 893018995..a0cee9d83 100644 --- a/web/src/hooks/use-firebase.ts +++ b/web/src/hooks/use-firebase.ts @@ -1,14 +1,29 @@ -import { firebaseConfig } from "@/types/notifications"; -import { getMessaging, getToken } from "firebase/messaging"; +import { getMessaging } from "firebase/messaging"; import { initializeApp } from "firebase/app"; -import { useMemo } from "react"; +import { useEffect, useMemo, useState } from "react"; export function useFirebaseApp() { + const [firebaseConfig, setFirebaseConfig] = useState(); + + useEffect(() => { + if (!firebaseConfig) { + fetch(`${window.location.href}/firebase-config.json`).then( + async (resp) => { + setFirebaseConfig(await resp.json()); + }, + ); + } + }, [firebaseConfig]); + return useMemo(() => { + if (!firebaseConfig) { + return; + } + const app = initializeApp(firebaseConfig); app.automaticDataCollectionEnabled = false; return app; - }, []); + }, [firebaseConfig]); } export function useFirebaseMessaging() { diff --git a/web/src/types/notifications.ts b/web/src/types/notifications.ts deleted file mode 100644 index c24111c7a..000000000 --- a/web/src/types/notifications.ts +++ /dev/null @@ -1,9 +0,0 @@ -export const firebaseConfig = { - apiKey: "AIzaSyCoweRLtvai8iNwhsoT-GH_CH_0pckqMmA", - authDomain: "frigate-ed674.firebaseapp.com", - projectId: "frigate-ed674", - storageBucket: "frigate-ed674.appspot.com", - messagingSenderId: "76314288339", - appId: "1:76314288339:web:090e170610d3bf0966f426", - measurementId: "G-GZ1JKNDJZK", -};