From 7146f9af88cb80b4021c5bd555d51792d12cbb10 Mon Sep 17 00:00:00 2001 From: Thies Mueller Date: Sat, 16 May 2026 17:14:00 +0200 Subject: [PATCH] app.js local storage --- app.js | 144 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 97 insertions(+), 47 deletions(-) diff --git a/app.js b/app.js index 47b2468..bb992e0 100644 --- a/app.js +++ b/app.js @@ -3,6 +3,7 @@ require("dotenv").config(); const express = require("express"); const cors = require("cors"); const admin = require("firebase-admin"); +const fs = require("fs"); const app = express(); @@ -17,7 +18,31 @@ admin.initializeApp({ const API_KEY = process.env.API_KEY; +const TOKEN_FILE = "./tokens.json"; + +function loadTokens() { + try { + if (!fs.existsSync(TOKEN_FILE)) return []; + const data = fs.readFileSync(TOKEN_FILE, "utf8"); + return JSON.parse(data || "[]"); + } catch (err) { + console.error("[ERROR] Failed to load tokens:", err); + return []; + } +} + +function saveTokens(tokens) { + try { + fs.writeFileSync(TOKEN_FILE, JSON.stringify(tokens, null, 2)); + } catch (err) { + console.error("[ERROR] Failed to save tokens:", err); + } +} + +let deviceTokens = new Set(loadTokens()); + function authMiddleware(req, res, next) { + console.log("[INFO] Incoming request to /push"); const authHeader = req.headers.authorization; if (!authHeader) { @@ -37,13 +62,37 @@ function authMiddleware(req, res, next) { next(); } +app.post("/register-token", (req, res) => { + console.log("[INFO] /register-token called"); + + const { token, platform } = req.body; + + if (!token) { + return res.status(400).json({ + error: "token required", + }); + } + + deviceTokens.add(token); + + saveTokens([...deviceTokens]); + + console.log("[INFO] Token registered"); + console.log("[DEBUG] Token:", token); + console.log("[DEBUG] Platform:", platform || "unknown"); + console.log("[INFO] Total tokens:", deviceTokens.size); + + res.json({ + success: true, + count: deviceTokens.size, + }); +}); + app.post("/push", authMiddleware, async (req, res) => { + console.log("[INFO] /push called"); + try { - const { - type = "none", - title, - message, - } = req.body; + const { type = "none", title, message } = req.body; if (!title || !message) { return res.status(400).json({ @@ -51,72 +100,69 @@ app.post("/push", authMiddleware, async (req, res) => { }); } - let color = "#2196f3"; + if (deviceTokens.size === 0) { + return res.status(400).json({ + error: "No registered device tokens", + }); + } + + let color = "#9e9e9e"; switch (type) { case "info": color = "#2196f3"; break; - case "warning": color = "#ff9800"; break; - case "danger": color = "#f44336"; break; - - case "none": - default: - color = "#9e9e9e"; - break; } - const payload = { - topic: "global", + const messages = []; - notification: { - title, - body: message, - }, - - data: { - type, - color, - }, - - android: { - priority: "high", + for (const token of deviceTokens) { + messages.push({ + token, notification: { + title, + body: message, + }, + data: { + type, color, }, - }, - - apns: { - payload: { - aps: { - sound: "default", + android: { + priority: "high", + notification: { color }, + }, + apns: { + payload: { + aps: { sound: "default" }, }, }, - }, - - webpush: { - notification: { - icon: "/icon-192.png", + webpush: { + notification: { + icon: "/icon-192.png", + }, }, - }, - }; + }); + } - const response = await admin - .messaging() - .send(payload); + console.log("[INFO] Sending messages..."); + const response = await admin.messaging().sendEach(messages); + + console.log("[INFO]:", response.successCount); + console.log("[ERROR]:", response.failureCount); res.json({ success: true, - firebaseResponse: response, + successCount: response.successCount, + failureCount: response.failureCount, }); } catch (err) { - console.error(err); + console.error("[ERROR] PUSH:", err); res.status(500).json({ error: err.message, @@ -127,5 +173,9 @@ app.post("/push", authMiddleware, async (req, res) => { const PORT = process.env.PORT || 3000; app.listen(PORT, () => { - console.log(`Server running on ${PORT}`); -}); + console.log("================================="); + console.log("Push Server started"); + console.log("Port:", PORT); + console.log("Tokens:", TOKEN_FILE); + console.log("================================="); +}); \ No newline at end of file