【问题标题】:Send FCM notification to more than 1000 tokens node js向1000多个token节点js发送FCM通知
【发布时间】:2020-07-12 11:34:49
【问题描述】:

我有 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);
    }
  });

【问题讨论】:

    标签: javascript node.js firebase firebase-cloud-messaging


    【解决方案1】:

    有两种方法可以做到这一点。第一种方式发送 1000 然后 1000 ..等等。第二种方式是发送到特定主题,所有订阅该主题的客户都会收到您的通知。

    1. device-group
    2. topic-messaging

    此代码发送 1000 然后 1000 ..etc。但我不喜欢它。你应该使用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);
    }
    

    【讨论】:

      【解决方案2】:

      您需要将消息发送至batches

      例如:

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

      【讨论】:

        猜你喜欢
        • 2019-10-26
        • 1970-01-01
        • 1970-01-01
        • 2017-03-05
        • 2017-11-08
        • 2019-07-19
        • 2016-08-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多