1

client我想在创建文档时向 2 种不同类型的用户发送通知。首先,我必须搜索providers需要附近客户的附近提供商的集合,以及不关心距离的远程提供商,以便我可以向所有他们发送通知。我尝试实现这两个查询并发送通知,但失败了。错误消息是Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array。以下代码有什么问题?

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 query1 = remoteCollection.where("wantNear", "==", 'false')
        .where("field", "==", clientField);
    const query2 = nearCollection.near({center: clientGeopoint, radius: 10}).where("wantNear", "==", 'true')
        .where("field", "==", clientField);
    
    const tokenArray = [];

    const [remotePro, nearPro] = await Promise.all([query1.get(), query2.get()]);

    remotePro.docs.forEach((doc) => {
        const token1 = doc.data().fcmToken;
        tokenArray.push(token1);

    nearPro.docs.forEach((doc) => {
        const token2 = doc.data().fcmToken;
        tokenArray.push(token2);
      });
    const message = {
      "notification": {
        title: ...,
        body: ...,
      },
     };
    admin.messaging().sendToDevice(tokenArray, message);
  } catch (error) {
    console.log(error);
  }
});
4

0 回答 0