feat(fcm): add Firebase Admin SDK and messaging export

Wire firebase-admin with ADC or FIREBASE_SERVICE_ACCOUNT_JSON,
export messaging from src/services/firebase.ts, and load it at
server startup.
This commit is contained in:
Jose Olarte III
2026-05-11 14:47:05 +08:00
parent 94c38bac74
commit d311b6a504
4 changed files with 1963 additions and 4 deletions
+1941 -3
View File
File diff suppressed because it is too large Load Diff
+2 -1
View File
@@ -9,7 +9,8 @@
"build": "tsc" "build": "tsc"
}, },
"dependencies": { "dependencies": {
"express": "^5.1.0" "express": "^5.1.0",
"firebase-admin": "^13.9.0"
}, },
"devDependencies": { "devDependencies": {
"@types/express": "^5.0.0", "@types/express": "^5.0.0",
+1
View File
@@ -1,4 +1,5 @@
import express from "express"; import express from "express";
import "./services/firebase.js";
import { notificationsRouter } from "./routes/notifications.js"; import { notificationsRouter } from "./routes/notifications.js";
import { startScheduler } from "./scheduler.js"; import { startScheduler } from "./scheduler.js";
+19
View File
@@ -0,0 +1,19 @@
import admin from "firebase-admin";
import type { ServiceAccount } from "firebase-admin/app";
import type { Messaging } from "firebase-admin/messaging";
function resolveCredential(): admin.credential.Credential {
const json = process.env.FIREBASE_SERVICE_ACCOUNT_JSON;
if (json !== undefined && json.trim() !== "") {
return admin.credential.cert(JSON.parse(json) as ServiceAccount);
}
return admin.credential.applicationDefault();
}
if (!admin.apps.length) {
admin.initializeApp({
credential: resolveCredential(),
});
}
export const messaging: Messaging = admin.messaging();