mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 00:24:53 +00:00
NOtification update
This commit is contained in:
@ -16,8 +16,14 @@ exports.sendNotificationOnEventCreation = functions.firestore
|
||||
.document('Events/{eventId}')
|
||||
.onCreate(async (snapshot, context) => {
|
||||
const eventData = snapshot.data();
|
||||
const { familyId, creatorId } = eventData;
|
||||
|
||||
const pushTokens = await getPushTokensForEvent(eventData);
|
||||
if (!familyId || !creatorId) {
|
||||
console.error('Missing familyId or creatorId in event data');
|
||||
return;
|
||||
}
|
||||
|
||||
const pushTokens = await getPushTokensForFamilyExcludingCreator(familyId, creatorId);
|
||||
|
||||
if (!pushTokens.length) {
|
||||
console.log('No push tokens available for the event.');
|
||||
@ -36,7 +42,7 @@ exports.sendNotificationOnEventCreation = functions.firestore
|
||||
sound: 'default',
|
||||
title: 'New Event Added!',
|
||||
body: `An event "${eventData.title}" has been added. Check it out!`,
|
||||
data: {eventId: context.params.eventId},
|
||||
data: { eventId: context.params.eventId },
|
||||
});
|
||||
}
|
||||
|
||||
@ -45,7 +51,6 @@ exports.sendNotificationOnEventCreation = functions.firestore
|
||||
for (let chunk of chunks) {
|
||||
try {
|
||||
let ticketChunk = await expo.sendPushNotificationsAsync(chunk);
|
||||
|
||||
tickets.push(...ticketChunk);
|
||||
|
||||
for (let ticket of ticketChunk) {
|
||||
@ -53,7 +58,6 @@ exports.sendNotificationOnEventCreation = functions.firestore
|
||||
console.log('Notification successfully sent:', ticket.id);
|
||||
} else if (ticket.status === 'error') {
|
||||
console.error(`Notification error: ${ticket.message}`);
|
||||
|
||||
if (ticket.details && ticket.details.error) {
|
||||
console.error('Error details:', ticket.details.error);
|
||||
if (ticket.details.error === 'DeviceNotRegistered') {
|
||||
@ -63,12 +67,12 @@ exports.sendNotificationOnEventCreation = functions.firestore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error sending notification:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieve and handle notification receipts
|
||||
let receiptIds = [];
|
||||
for (let ticket of tickets) {
|
||||
if (ticket.id) {
|
||||
@ -84,7 +88,6 @@ exports.sendNotificationOnEventCreation = functions.firestore
|
||||
|
||||
for (let receiptId in receipts) {
|
||||
let { status, message, details } = receipts[receiptId];
|
||||
|
||||
if (status === 'ok') {
|
||||
console.log(`Notification with receipt ID ${receiptId} was delivered successfully`);
|
||||
} else if (status === 'error') {
|
||||
@ -99,7 +102,6 @@ exports.sendNotificationOnEventCreation = functions.firestore
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
@ -207,3 +209,18 @@ async function getPushTokensForEvent() {
|
||||
return pushTokens;
|
||||
}
|
||||
|
||||
async function getPushTokensForFamilyExcludingCreator(familyId, creatorId) {
|
||||
const usersRef = db.collection('Profiles');
|
||||
const snapshot = await usersRef.where('familyId', '==', familyId).get();
|
||||
let pushTokens = [];
|
||||
|
||||
snapshot.forEach(doc => {
|
||||
const data = doc.data();
|
||||
// Exclude the creator
|
||||
if (data.uid !== creatorId && data.pushToken) {
|
||||
pushTokens.push(data.pushToken);
|
||||
}
|
||||
});
|
||||
|
||||
return pushTokens;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user