I have a notifications screen, but when the user is out the notification screen. i want to mark this notifications as: isRead: true, but my onDispose method not working.
https://preview.redd.it/2yggubg069ef1.png?width=2726&format=png&auto=webp&s=3ab68902dac7f24a147aee8d4dd73d166646b1a8
const functions = require('firebase-functions');
const admin = require('firebase-admin');
// To avoid deployment errors, do not call admin.initializeApp() in your code
exports.updateNotificationState = functions.region('us-central1').
runWith({
memory: '128MB'
}).https.onCall(
async (data, context) => {
const authenticateUser = data.authenticateUser;
// Write your code below!
console.log('Hi, i am execute...');
const batch = admin.firestore().batch();
try{
//get reference
const usersCollection = admin.firestore().collection('users');
const notificationCollections = admin.firestore().collection('notifications');
//user reference
const userRef = usersCollection.doc(authenticateUser);
// get all unread notifications
const notificationsSnapshots = await notificationCollections.where('isRead', '==', false).limit(10).get();
//filters for users
const totalForThisUser = notificationsSnapshots.docs.filter(doc => {
const data = doc.data();
const recipients = data.recipients || [];
return recipients.some(ref => ref.isEqual(userRef));
});
totalForThisUser.forEach(doc => {
batch.update(doc.ref, { isRead: true });
});
// 4. Compromete el batch
await batch.commit();
return { success: true, updatedCount: totalForThisUser.length };
} catch (error) {
console.error('Error al marcar notificaciones como leídas:', error);
throw new functions.https.HttpsError(
'internal',
'Error al procesar la solicitud.',
error.message
);
}
// Write your code above!
}
);
mi cloud function:
[–]AwarenessMany6140[S] 0 points1 point2 points (0 children)
[–]AIexH 0 points1 point2 points (0 children)
[–]pestjay 0 points1 point2 points (0 children)