Php Android-GCM数据包发送
我可以使用一个请求发送多个推送通知吗?可以。我使用以下代码以1000个用户为单位发送它们:Php Android-GCM数据包发送,php,android,google-cloud-messaging,Php,Android,Google Cloud Messaging,我可以使用一个请求发送多个推送通知吗?可以。我使用以下代码以1000个用户为单位发送它们: $url = 'https://android.googleapis.com/gcm/send'; $fields['data'] = $message; $headers = array( 'Authorization: key=' . $api_key, 'Content-Type: application/json' ); //echo "Reg ids: ".va
$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>";
$url='1!'https://android.googleapis.com/gcm/send';
$fields['data']=$message;
$headers=数组(
“授权:密钥=”。$api_密钥,
'内容类型:应用程序/json'
);
//echo“Reg id:.var_dump($fields['registration_id'])。“
”;
对于($i=0;$i<9998;$i++){
$registration_id[$i+2]=$i;
}
echo“发送给GCM的必要请求总数:”(intval(count($registration_id)/1000)+1)。“
”;
对于($j=0;$j”;
$fields['registration_id']=array();
对于($i=0;$i<1000&($i+1000*$j)”;
//开放连接
$ch=curl_init();
//设置url、POST变量数和POST数据
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//暂时禁用SSL证书支持
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
//变量转储($fields);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
//执行职务
$result=curl\u exec($ch);
//$result=true;
如果($result==FALSE){
模具('Curl失败:'。Curl_错误($ch));
}
其他的
呼应“卷曲成功
”;
回显$result。“
”;
我希望您觉得此代码有用是的,您可以。我使用此代码以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>";
$url='1!'https://android.googleapis.com/gcm/send';
$fields['data']=$message;
$headers=数组(
“授权:密钥=”。$api_密钥,
'内容类型:应用程序/json'
);
//echo“Reg id:.var_dump($fields['registration_id'])。“
”;
对于($i=0;$i<9998;$i++){
$registration_id[$i+2]=$i;
}
echo“发送给GCM的必要请求总数:”(intval(count($registration_id)/1000)+1)。“
”;
对于($j=0;$j”;
$fields['registration_id']=array();
对于($i=0;$i<1000&($i+1000*$j)”;
//开放连接
$ch=curl_init();
//设置url、POST变量数和POST数据
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//暂时禁用SSL证书支持
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
//变量转储($fields);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
//执行职务
$result=curl\u exec($ch);
//$result=true;
如果($result==FALSE){
模具('Curl失败:'。Curl_错误($ch));
}
其他的
呼应“卷曲成功
”;
回显$result。“
”;
我希望这段代码对您有用我们可以向多达1000台设备发送相同的消息。
但是我们不能通过一个请求向不同的设备发送不同的通知。我们最多可以向1000台设备发送相同的消息。
但我们不能用一个请求向不同的设备发送不同的通知。是的。你们可以。但一个接一个。不是一堆。你们可以做一个循环并发送它。是的。你们可以。但一个接一个。不是一堆。你们可以做一个循环并发送它。谢谢,但我的意思是通过一个请求发送。所以根据docs和@Chintan Rathod,发送是可能的每个请求只能为不同的设备发送相同的通知。你不能说它是不同的设备。我认为你有点像点对点通知一样的推送通知的含义。但GCM并不是这个意思。如果你从服务器发出任何推送通知,它会将其发送到注册了GCM的设备上。只是澄清一下。我有2个不同的设备已注册,我想向第一个设备发送通知“你好”,向第二个设备发送通知“你好”。只需一个请求。可能吗?@DmitryTeplyakov,回答:不可能。在从服务器逐个发送消息时,您必须将消息分成两部分。就像您需要在数据库中设置一些标志,以获取该“值”从数据库并基于它发送消息。好的,我现在知道了,有两个可能的答案:您可以通过一个请求向多达1000个设备发送相同的消息,但不能向同一个设备发送两条不同的消息。例如,这是由于他们正在检查的json对象的性质。其中不能包含json对象数组要发送两个不同的推送,您需要curl_exec()两次。谢谢,但我的意思是通过一个请求发送。因此,根据docs和@Chintan Rathod的说法,每个请求只能为不同的设备发送相同的通知。你不能说它是不同的设备。我认为你有推送通知的含义,就像点对点通知一样。但GCM不是这个意思。如果你发射任何推送通知从服务器开始,它将尽可能多地将其发送到在GCM注册的设备。只是澄清一下。我注册了两个不同的设备,我想向第一个设备发送通知“你好”和“你好”通过一个请求发送到第二个。可能吗?@DmitryTeplyakov,回答:不可能。在从服务器逐个发送消息时,您必须将消息分成两部分。就像您需要在数据库中设置一些标志,以获取该“值”从数据库并根据它发送消息。好的,我现在知道了,有两个可能的答案:您可以将相同的消息发送到最多100个