如何同时向不同的主题发送 FCM 多条通知消息



我检查了很多答案,它们都是关于向不同的主题发送通知,但我想一次向不同的主题发送不同的消息。

我有 100+ 个主题要发送通知,一个一个发送需要太多时间才能发送。

我没有找到任何 API 文档,所以我尝试了不同的技术,例如$fields发送多维数组,但它返回错误。

我的代码一次只发送 1 个通知。

如何在单个请求中发送所有消息?

// Sending through PHP Curl request
$url = 'https://fcm.googleapis.com/fcm/send';
$msgs = array();
$msgs["topic_1"] = array("title"=>"title 1","body"=>"topic 1 message");
$msgs["topic_2"] = array("title"=>"title 2","body"=>"topic 2 message");
$msgs["topic_3"] = array("title"=>"title 3","body"=>"topic 3 message");
...
$msgs["topic_100"] = array("title"=>"title 100","body"=>"topic 100 message");

foreach ($msgs as $topic => $message) {
$fields = array(
'to' => "/topics/$topic",
"priority"=> "high",
'data' => $message,
"time_to_live" => 7200
);
$headers = array('Authorization:key=API_KEY',
'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);           
curl_close($ch);
} // loop end
//above code only send 1 message at once..

参考此文档

您可以在按摩中按使用条件参数向多个主题发送通知

例如,以下条件将向订阅了主题 A 和主题 B 或主题 C 的设备发送消息:

"主题中的'主题A'&

&&(主题中的'主题B'||主题C'在主题中(">

完整示例:

// Define a condition which will send to devices which are subscribed
// to either the Google stock or the tech industry topics.
var condition = "'stock-GOOG' in topics || 'industry-tech' in topics";
// See documentation on defining a message payload.
var message = {
notification: {
title: '$GOOG up 1.43% on the day',
body: '$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day.'
},
condition: condition
};
// Send a message to devices subscribed to the combination of topics
// specified by the provided condition.
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log('Successfully sent message:', response);
})
.catch((error) => {
console.log('Error sending message:', error);
});

注意:您最多可以在条件中包含五个主题 表达。

相关内容

  • 没有找到相关文章

最新更新