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:
1944
package-lock.json
generated
1944
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -9,7 +9,8 @@
|
||||
"build": "tsc"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^5.1.0"
|
||||
"express": "^5.1.0",
|
||||
"firebase-admin": "^13.9.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^5.0.0",
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import express from "express";
|
||||
import "./services/firebase.js";
|
||||
import { notificationsRouter } from "./routes/notifications.js";
|
||||
import { startScheduler } from "./scheduler.js";
|
||||
|
||||
|
||||
19
src/services/firebase.ts
Normal file
19
src/services/firebase.ts
Normal 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();
|
||||
Reference in New Issue
Block a user