php不同设备发不同的消息,php - 如何同时向不同主题发送FCM多个通知消息 - SO中文参考 - www.soinside.com...

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

我有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..

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值