提问者:小点点

向1000多个令牌节点js发送FCM通知


我有5000+用户令牌的firestore文件,但FCM限制是1000,我如何发送通知所有。

我如何发送1000-1000使用循环任何人都可以帮助我解决这个问题。

var newData;

exports.articlenotification = functions.firestore
  .document("Articles/{id}")
  .onCreate(async (snapshot, context) => {
    //

    if (snapshot.empty) {
      console.log("No Devices");
      return;
    }

    newData = snapshot.data();

    const deviceIdTokens = await admin
      .firestore()
      .collection("Tokens")
      .where("article", "==", true)
      .get();

    var tokens = [];

    for (var token of deviceIdTokens.docs) {
      tokens.push(token.data().token);
    }
    var payload = {
      notification: {
        title: "New Article",
        body: newData.title,
        image: newData.image,
        sound: "default"
      }
    };

    try {
      const response = await admin.messaging().sendToDevice(tokens, payload);
      console.log("Notification sent successfully");
    } catch (err) {
      console.log(err);
    }
  });

共2个答案

匿名用户

您需要分批发送消息。

例如:

// Create a list containing up to 500 messages.
const messages = [];
messages.push({
  notification: {title: 'Price drop', body: '5% off all electronics'},
  token: registrationToken,
});
messages.push({
  notification: {title: 'Price drop', body: '2% off all books'},
  topic: 'readers-club',
});

admin.messaging()
.sendAll(messages)
.then((response) => console.log(response.successCount + ' messages were sent successfully'));

匿名用户

有两种方法可以做到。 首先发送1000,然后发送1000.。。等等。 第二种方式是发送到特定主题,所有订阅此主题的客户端都将收到您的通知。

  1. 设备组
  2. 主题-消息

此代码发送1000然后1000..等。 但我不喜欢。 您应该使用topic-messaging来代替它。

for (let i = 0; i < listOf5000Tokens.length; i += 1000) {
    const listOf1000Tokens = listOf5000Tokens.slice(i, i + 1000);

    // using await to wait for sending to 1000 token
    await doSomeThing(listOf1000Tokens);
}