【发布时间】:2019-08-21 19:52:56
【问题描述】:
我检查了很多答案,它们都是关于向不同主题发送通知的,但我想一次向不同主题发送不同的消息。
我有 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..
【问题讨论】:
标签: php android firebase notifications firebase-cloud-messaging