首页 > 解决方案 > firebase云函数如何调用清理函数

问题描述

我在 db 中有通知令牌,超过 10k,我想像这样发送数据消息:

const promises = [];

      l = niz.length;
      for (i = 0; i < l; i++) {
        promises.push(admin.messaging().sendToDevice(niz[i], payload, options));
      }

      return Promise.all(promises);

在 niz 我有令牌数组,长度为 999。如何从此代码调用清理函数,这将删除无效的令牌。我需要响应和 niz[i] 发送到清理功能。但我不知道怎么做,因为我发送的承诺不止一个......

标签: firebasegoogle-cloud-functions

解决方案


您可以检查每条消息是否有任何错误。

    const promises = [];

    l = niz.length;
    for (i = 0; i < l; i++) {
        promises.push(admin.messaging().sendToDevice(niz[i], payload, options));
    }

    return Promise.all(promises).then((response) => {
        // For each message check if there was an error.
        const tokensToRemove = [];
        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') {
                    // Remove Token : tokens[index]
                }
            }
        });
        return Promise.all(tokensToRemove);
    });

您可以在FCM 发送通知中找到完整的解决方案


推荐阅读