我可以使用Android GCM通过一个请求发送多个推送通知吗?
问问题
673 次
2 回答
1
是的你可以。我使用以下代码以 1000 个用户为一组发送它们:
$url = 'https://android.googleapis.com/gcm/send';
$fields['data'] = $message;
$headers = array(
'Authorization: key=' . $api_key,
'Content-Type: application/json'
);
//echo "Reg ids: ".var_dump($fields['registration_ids'])."<br>";
for ($i = 0; $i < 9998; $i++) {
$registration_ids[$i+2] = $i;
}
echo "Total necessary requests to GCM: ".(intval(count($registration_ids)/1000)+1)."<br>";
for ($j = 0; $j < count($registration_ids)/1000; $j++) {
echo "Processing request number ".($j + 1)."...<br>";
$fields['registration_ids'] = array();
for($i=0;$i < 1000 && ($i+1000*$j) < count($registration_ids);$i++){
$fields['registration_ids'][$i] = $registration_ids[$i+1000*$j];
}
echo "Sending push to devices: ".(1000*$j)." - ".(count($fields['registration_ids'])+1000*$j)."<br>";
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//var_dump($fields);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
//$result = true;
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
else
echo "Curl success<br>";
echo $result."<br>";
我希望你觉得这段代码有用
于 2013-06-17T09:10:13.893 回答
0
我们可以向多达 1000 台设备发送相同的消息。 https://developers.google.com/cloud-messaging/http-server-ref#table1
但是我们不能通过一个请求向不同的设备发送不同的通知。
于 2016-07-24T21:38:17.010 回答