Trying to do the notification for all users.

This commit is contained in:
Van Leemput Dayron
2025-12-03 17:32:06 +01:00
parent fd19b88eef
commit a74d76b485
10 changed files with 505 additions and 227 deletions

View File

@@ -5,21 +5,32 @@ admin.initializeApp();
// Helper function to send notifications to a list of users
async function sendNotificationToUsers(userIds, title, body, excludeUserId, data = {}) {
console.log(`Starting sendNotificationToUsers. Total users: ${userIds.length}, Exclude: ${excludeUserId}`);
try {
const tokens = [];
for (const userId of userIds) {
if (userId === excludeUserId) continue;
if (userId === excludeUserId) {
console.log(`Skipping user ${userId} (sender)`);
continue;
}
const userDoc = await admin.firestore().collection("users").doc(userId).get();
if (userDoc.exists) {
const userData = userDoc.data();
if (userData.fcmToken) {
console.log(`Found token for user ${userId}`);
tokens.push(userData.fcmToken);
} else {
console.log(`No FCM token found for user ${userId}`);
}
} else {
console.log(`User document not found for ${userId}`);
}
}
console.log(`Total tokens to send: ${tokens.length}`);
if (tokens.length > 0) {
const message = {
notification: {
@@ -35,6 +46,11 @@ async function sendNotificationToUsers(userIds, title, body, excludeUserId, data
const response = await admin.messaging().sendMulticast(message);
console.log(`${response.successCount} messages were sent successfully`);
if (response.failureCount > 0) {
console.log('Failed notifications:', response.responses.filter(r => !r.success));
}
} else {
console.log("No tokens found, skipping notification send.");
}
} catch (error) {
console.error("Error sending notification:", error);
@@ -44,6 +60,7 @@ async function sendNotificationToUsers(userIds, title, body, excludeUserId, data
exports.onActivityCreated = functions.firestore
.document("activities/{activityId}")
.onCreate(async (snapshot, context) => {
console.log(`onActivityCreated triggered for ${context.params.activityId}`);
const activity = snapshot.data();
const tripId = activity.tripId;
const createdBy = activity.createdBy || "Unknown";
@@ -66,6 +83,8 @@ exports.onActivityCreated = functions.firestore
participants.push(trip.createdBy);
}
console.log(`Found trip participants: ${JSON.stringify(participants)}`);
// Fetch creator name
let creatorName = "Quelqu'un";
if (createdBy !== "Unknown") {
@@ -87,6 +106,7 @@ exports.onActivityCreated = functions.firestore
exports.onMessageCreated = functions.firestore
.document("groups/{groupId}/messages/{messageId}")
.onCreate(async (snapshot, context) => {
console.log(`onMessageCreated triggered for ${context.params.messageId} in group ${context.params.groupId}`);
const message = snapshot.data();
const groupId = context.params.groupId;
const senderId = message.senderId;
@@ -100,6 +120,7 @@ exports.onMessageCreated = functions.firestore
const group = groupDoc.data();
const memberIds = group.memberIds || [];
console.log(`Found group members: ${JSON.stringify(memberIds)}`);
let senderName = message.senderName || "Quelqu'un";
@@ -115,6 +136,7 @@ exports.onMessageCreated = functions.firestore
exports.onExpenseCreated = functions.firestore
.document("expenses/{expenseId}")
.onCreate(async (snapshot, context) => {
console.log(`onExpenseCreated triggered for ${context.params.expenseId}`);
const expense = snapshot.data();
const groupId = expense.groupId;
const paidBy = expense.paidById || expense.paidBy;
@@ -133,6 +155,7 @@ exports.onExpenseCreated = functions.firestore
const group = groupDoc.data();
const memberIds = group.memberIds || [];
console.log(`Found group members: ${JSON.stringify(memberIds)}`);
let payerName = expense.paidByName || "Quelqu'un";