gpt4 book ai didi

php - Android GCM 数据包发送

转载 作者:行者123 更新时间:2023-11-30 03:32:52 26 4
gpt4 key购买 nike

我可以使用 Android GCM 通过一个请求发送多个推送通知吗? ?

最佳答案

是的,你可以。我使用以下代码将它们以 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>";

希望这段代码对你有用

关于php - Android GCM 数据包发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17143485/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com