简体   繁体   English

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

[英]Send FCM notification to more than 1000 tokens node js

I have firestore document with 5000+ users token but FCM limit is 1000 how can I send notification to all.我有带有 5000 多个用户令牌的 Firestore 文档,但 FCM 限制为 1000,如何向所有人发送通知。

how can I send 1000-1000 using loop anyone can help me to figure out this.我如何使用循环发送 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);
    }
  });

you'll need to send the message in batches .您需要分批发送消息。

For example:例如:

// 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'));

There are two ways to do it.有两种方法可以做到这一点。 first way to sent 1000 then 1000..etc.第一种方式发送 1000 然后 1000..等等。 and second way by send to specific topic and all clients who subscribe to this topic will receive your notification.第二种方式是发送到特定主题,所有订阅该主题的客户都会收到您的通知。

  1. device-group 设备组
  2. topic-messaging 主题消息

This code to send 1000 then 1000..etc.此代码发送 1000 然后 1000..etc。 but I don't prefer it.但我不喜欢它。 you should use topic-messaging instead of it.你应该使用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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM