2021年3月12日星期五

cloud functions-firestore: multiple query

I want to send notifications to 2 different types of users when a client document is created. First, I have to search providers collection for nearby providers who want nearby clients, and for remote providers who don't care about distance so that I can send notifications to all of them. I tried to implement these 2 queries, and send notifications, but failed. The error message was Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array. What is wrong with this following code?

exports.clientRegisterNotification = functions.firestore  .document("crews/{crew}/clients/{client}")  .onCreate(async (snapshot) => {    try {      const clientGeopoint = snapshot.data().g.geopoint;      const clientField = snapshot.data().field;        const nearCollection = geofirestore.collectionGroup("providers");      const remoteCollection = firestore.collectionGroup("providers");            const tokenArray = [];      const promises = [];        const query1 = remoteCollection.where("wantNear", "==", 'false')          .where("field", "==", clientField);      const query2 = nearCollection.near({center: clientGeopoint, radius: 10}).where("wantNear", "==", 'true')          .where("field", "==", clientField);            const [remotePro, nearPro] = await Promise.all([query1.get(), query2.get()]);        remotePro.docs.forEach((doc) => {          const token = doc.data().fcmToken;          promises.push(tokenArray.push(token));        nearPro.docs.forEach((doc) => {          const token = doc.data().fcmToken;          promises.push(tokenArray.push(token));        });      const message = {        "notification": {          title: ...,          body: ...,        },       };      Promise.all(promises).then((tokenArray) => admin.messaging().sendToDevice(tokenArray, message)).catch((e) => console.log(e));    } catch (error) {      console.log(error);    }  });  
https://stackoverflow.com/questions/66595505/cloud-functions-firestore-multiple-query March 12, 2021 at 02:54PM

没有评论:

发表评论