I am storing the firebase cloud messaging registration tokens for the users in the firestore users collection and whenever I'm sending a notification, I'm deleting the tokens which are unused. I'm doing this on the cloud function with admin sdk. The code I'm using is-
let removeRegistrationTokens = async function (userId: string, tokens: string[]) {
await db.collection('users')
.doc(userId)
.update("tokens",admin.firestore.FieldValue.arrayRemove(tokens)
.catch(logError)
}
But this is not working and no value is being deleted from the array. I checked the tokens array and the value passed is correct. If I simply change the code to
.update("tokens",admin.firestore.FieldValue.arrayRemove(tokens[0])
This is deleting the first element in the array from the tokens array in firestore. As per the documentation it should delete all the elements passed to it in the array.
If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present. arrayRemove() removes all instances of each given element.
Any help on why could it be happening?