您好我正在开发一个通知系统,但我无法删除已处理的通知数据。事件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);
});
});
})