Added notifciations

This commit is contained in:
Milan Paunovic
2024-11-04 01:01:59 +01:00
parent 848211c3c8
commit 84a974f3f7
8 changed files with 153 additions and 16 deletions

View File

@ -1,6 +1,6 @@
const {onRequest} = require("firebase-functions/v2/https");
const {getAuth} = require("firebase-admin/auth");
const {getFirestore} = require("firebase-admin/firestore");
const {getFirestore, Timestamp} = require("firebase-admin/firestore");
const logger = require("firebase-functions/logger");
const functions = require('firebase-functions');
const admin = require('firebase-admin');
@ -22,7 +22,7 @@ exports.sendNotificationOnEventCreation = functions.firestore
.document('Events/{eventId}')
.onCreate(async (snapshot, context) => {
const eventData = snapshot.data();
const { familyId, creatorId, email } = eventData;
const { familyId, creatorId, email, title } = eventData;
if (!familyId || !creatorId) {
console.error('Missing familyId or creatorId in event data');
@ -44,7 +44,7 @@ exports.sendNotificationOnEventCreation = functions.firestore
notificationTimeout = setTimeout(async () => {
const eventMessage = eventCount === 1
? `An event "${eventData.title}" has been added. Check it out!`
? `An event "${title}" has been added. Check it out!`
: `${eventCount} new events have been added.`;
let messages = pushTokens.map(pushToken => {
@ -85,6 +85,22 @@ exports.sendNotificationOnEventCreation = functions.firestore
}
}
// Save the notification in Firestore for record-keeping
const notificationData = {
creatorId,
familyId,
content: eventMessage,
eventId: context.params.eventId,
timestamp: Timestamp.now(),
};
try {
await db.collection("Notifications").add(notificationData);
console.log("Notification stored in Firestore:", notificationData);
} catch (error) {
console.error("Error saving notification to Firestore:", error);
}
// Reset state variables after notifications are sent
eventCount = 0;
pushTokens = [];