我有拥有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);
}
});
有两种方法。第一种方法是发送1000,然后发送1000。。第二种方式是发送到特定主题,所有订阅该主题的客户都会收到您的通知。
- 设备组
- 主题消息
此代码发送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);
}
您需要分批发送消息。
例如:
// 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'));