Привет, я разрабатываю систему уведомлений, но у меня возникают проблемы с удалением обработанных данных уведомлений. Прослушиватель событий onWrite
запускается дважды, что приводит к двум уведомлениям.
Можете ли вы помочь мне найти обходной путь, чтобы прослушиватель событий onWrite не запускался дважды? Важно удалить обработанные данные.
exports.sendMessageNotification = functions.database.ref('/notification/message/{recipientUid}/{senderUid}').onWrite(event => {
/* processing notification and sends FCM */
return admin.messaging().sendToDevice(tokens, payload).then(response => {
// For each message check if there was an error.
const toRemove = [];
response.results.forEach((result, index) => {
const error = result.error;
if (error) {
console.error('Failure sending notification to', tokens[index], error);
// Cleanup the tokens who are not registered anymore.
if (error.code === 'messaging/invalid-registration-token' ||
error.code === 'messaging/registration-token-not-registered') {
toRemove.push(tokensSnapshot.ref.child(tokens[index]).remove());
}
}
});
//Deletes processed notification
console.log("Removing notification");
const getNotificationPromise = admin.database().ref(`/notification/message/${recipientUid}/${senderUid}`).once('value');
return Promise.all([getNotificationPromise]).then(results => {
const notificationSnapshot = results[0];
toRemove.push(notificationSnapshot.ref.remove());
console.log("Removing tokens.")
return Promise.all(toRemove);
});
//return Promise.all(tokensToRemove);
});
});
})