Co-authored-by: MENDOZA BALLARDO GAEL RICARDO <gael-meb123@users.noreply.github.com>
Co-authored-by: Azareth-Tr <Azareth-Tr@users.noreply.github.com> Co-authored-by: eddgranados12 <eddgranados12@users.noreply.github.com> primeras vistas para el frontend, configuracion para firebase
This commit is contained in:
@@ -1,49 +1,43 @@
|
||||
import os
|
||||
from typing import Dict
|
||||
import firebase_admin
|
||||
from firebase_admin import credentials, messaging
|
||||
|
||||
_initialized = False
|
||||
_use_mock = True
|
||||
_firebase_initialized = False
|
||||
|
||||
def init_firebase(credentials_path: str = None):
|
||||
global _initialized, _use_mock
|
||||
try:
|
||||
import firebase_admin
|
||||
from firebase_admin import credentials, messaging
|
||||
except Exception:
|
||||
_use_mock = True
|
||||
return
|
||||
def init_firebase(cred_path: str):
|
||||
"""Inicializa la conexión con Firebase usando el Service Account (JSON)."""
|
||||
global _firebase_initialized
|
||||
|
||||
if os.path.exists(cred_path):
|
||||
try:
|
||||
cred = credentials.Certificate(cred_path)
|
||||
firebase_admin.initialize_app(cred)
|
||||
_firebase_initialized = True
|
||||
print(f"Firebase Admin SDK inicializado correctamente desde: {cred_path}")
|
||||
except ValueError:
|
||||
# Si el entorno se recarga, Firebase podría quejarse de que ya se inicializó
|
||||
_firebase_initialized = True
|
||||
except Exception as e:
|
||||
print(f"Error al inicializar Firebase: {e}")
|
||||
else:
|
||||
print(f"ADVERTENCIA: Credenciales no encontradas en '{cred_path}'.")
|
||||
print("Las notificaciones se ejecutarán en modo SIMULADO (solo consola).")
|
||||
|
||||
if credentials_path is None:
|
||||
credentials_path = os.environ.get('FIREBASE_CREDENTIALS_PATH', 'backend/secrets/firebase-adminsdk.json')
|
||||
|
||||
if not os.path.exists(credentials_path):
|
||||
_use_mock = True
|
||||
def send_to_topic(topic: str, title: str, body: str):
|
||||
"""Envía una notificación push a todos los dispositivos suscritos a un topic (ej. RUTA-01)."""
|
||||
if not _firebase_initialized:
|
||||
print(f"[MOCK PUSH] -> Topic: {topic} | Título: '{title}' | Mensaje: '{body}'")
|
||||
return
|
||||
|
||||
try:
|
||||
cred = credentials.Certificate(credentials_path)
|
||||
firebase_admin.initialize_app(cred)
|
||||
_initialized = True
|
||||
_use_mock = False
|
||||
except Exception:
|
||||
_use_mock = True
|
||||
|
||||
|
||||
def send_to_topic(topic: str, payload: Dict):
|
||||
"""Sends a push to an FCM topic. Falls back to mock (prints) if not configured."""
|
||||
global _use_mock
|
||||
if _use_mock:
|
||||
print(f"[MOCK PUSH] topic={topic} payload={payload}")
|
||||
return {"mock": True, "topic": topic, "payload": payload}
|
||||
|
||||
try:
|
||||
from firebase_admin import messaging
|
||||
message = messaging.Message(
|
||||
notification=messaging.Notification(title=payload.get('title'), body=payload.get('body')),
|
||||
notification=messaging.Notification(
|
||||
title=title,
|
||||
body=body,
|
||||
),
|
||||
topic=topic,
|
||||
)
|
||||
resp = messaging.send(message)
|
||||
return {"result": resp}
|
||||
response = messaging.send(message)
|
||||
print(f"Push enviado al topic '{topic}' exitosamente. MessageID: {response}")
|
||||
except Exception as e:
|
||||
print(f"[PUSH ERROR] {e}")
|
||||
return {"error": str(e)}
|
||||
print(f"Error al enviar push al topic '{topic}': {e}")
|
||||
Reference in New Issue
Block a user