I am working with an iOS app which calls an Firebase cloud function, to store FCM tokens, for later use when sending notifications. The problem is that it does not work.
I am using a Cloud Firestore database.
When the function is called, here is what I want to happen. The function checks the parameters against a database. If the data in the provided parameter is already found in the DB then nothing should happen. If it is not found, then it should be added to the DB.
My cloud function code is below. I would be glad if someone could help me find the precise issue.
exports.addNewElement = functions.https.onCall((data, context) => {
console.log('--addNewElement-- has been called with:\n' + data.fcmToken + '\n!!');
var collectionRef = db.collection("THE_COLLECTION");
// Create a query against the collection.
var query = collectionRef.where("fcmToken", "==", data.fcmToken);
query.get().then(function(doc) {
if (doc.exists) { // We should do nothing.
console.log("Document data:", doc.data());
} else { // We should add the new element.
// doc.data() will be undefined in this case
console.log("No such document!");
collectionRef.add({"fcmToken": fcmToken})
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
});
I could imagine other directions to handle FCM tokens. Is there any recommend way to use as best practice?