亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

向1000多個token節點js發送FCM通知

向1000多個token節點js發送FCM通知

絕地無雙 2022-11-11 16:58:21
我有帶有 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 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

有兩種方法可以做到這一點。第一種方式發送 1000 然后 1000 ..等等。第二種方式是發送到特定主題,所有訂閱該主題的客戶都會收到您的通知。

  1. 設備組

  2. 主題消息

此代碼發送 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);

}


查看完整回答
反對 回復 2022-11-11
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

您需要分批發送消息。


例如:


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




查看完整回答
反對 回復 2022-11-11
  • 2 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號