Php GCM丢弃消息
我这里有一个gcm的演示: 如果刷新页面,您将看到一个新的随机数。只有四分之一的页面刷新会向gcm发送消息。这是正常的还是我可以使用的代码有一些缺陷 该页未被缓存 代码:Php GCM丢弃消息,php,android,curl,google-cloud-messaging,Php,Android,Curl,Google Cloud Messaging,我这里有一个gcm的演示: 如果刷新页面,您将看到一个新的随机数。只有四分之一的页面刷新会向gcm发送消息。这是正常的还是我可以使用的代码有一些缺陷 该页未被缓存 代码: 有两种方法可以解决这个问题 生成并安装CA证书 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE) 不验证主机 该网站并不总是打印返回的JSON:谷歌在其中输入错误代码和一些问题(比如API密钥不正确,或者发送了太多请求等),请查看一下您是以JSON格式还是以编码形式发送数据?实际上,
有两种方法可以解决这个问题
生成并安装CA证书
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE)
不验证主机 该网站并不总是打印返回的JSON:谷歌在其中输入错误代码和一些问题(比如API密钥不正确,或者发送了太多请求等),请查看一下您是以JSON格式还是以编码形式发送数据?实际上,从while循环判断,您可能确实发送了太多的消息。GCM要求您实施指数退避。尝试向SQL添加限制1
,看看它是否有效。否则,看看返回的JSON,正如我上面提到的,是的,我正在对它们进行编码curl_setopt($ch,CURLOPT_POSTFIELDS,JSON_encode($fields));限制1只返回一个电话id,我需要所有的电话id填写数组并发送到GCMB,但您可能发送得太快了。它是否在100%的时间内发送限制1
?更好的方法是使用JSON并有多个ID发送到(请参见注册\u id
)此问题的另一个原因是来自Yahoo主机的CA证书。它是通过第三方租用的,因此它不存在于您的Web空间或您的帐户中。我必须通过Godaddy购买主机和证书。剧本现在很好用。
<?php
require_once '../include/DB_Functions.php';
// get database access
$db = new DB_Functions();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
//api key
$apiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$result;
//array for phones connected to this service
$registrationIDs = array();
$randomNum=rand(10,100);
echo "this is updateusers".$randomNum;
$_POST['message']="updateusers".$randomNum;
if (isset($_POST['message']) && $_POST['message'] != ''){
echo "<br>if (isset) updateusers".$randomNum;
// Message to be sent
$id= ''.mysql_real_escape_string(htmlentities($_POST['server_id'])).'';
$new_message= ''.mysql_real_escape_string(htmlentities($_POST['message'])).'';
// get client registration IDs
$query ="SELECT * FROM GoogleCloudMsg";
$queryresult=mysql_query($query);
while($row=mysql_fetch_assoc($queryresult)){
echo "<br>While loop updateusers".$randomNum;
$regId=$row['GCMPhoneRegisteredId'];
array_push($registrationIDs,$regId);
}
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields;
if(!$id || $id==""){
$fields =
array(
'registration_ids' => $registrationIDs,
'data' => array("message" => $new_message),
'delay_while_idle'=> false,
'collapse_key'=>"".$randomNum.""
);
echo "<br> id is blank updateusers".$randomNum;
}else{
$fields =
array(
'registration_ids' => $registrationIDs,
'data' => array("message" =>$new_message,"server_id"=>$id),
'delay_while_idle' => 'false',
'collapse_key'=>"".$randomNum.""
);
echo "<br>id exists updateusers".$randomNum;
}
$headers = array('Authorization: key=' . $apiKey, 'Content-Type: application/json');
// 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);
//curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_POST, true);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
echo $result;
}
echo "<br>mysql close updateusers".$randomNum;
mysql_close();
?>