我检查了很多答案,它们都是关于将通知发送到不同主题的,但是我想一次将不同消息发送到不同主题。
我有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..