【发布时间】:2012-10-13 21:25:30
【问题描述】:
我在这里有一个 gcm 的演示:http://leobee.com/android/push/login/gcm/updateusers.php
如果您刷新页面,您将看到一个新的随机数。 4 次页面刷新中只有 1 次向 gcm 发送消息。这是正常的还是我可以使用的代码有一些 tweeks?
页面没有被缓存。
代码:
<?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();
?>
【问题讨论】:
-
该站点并不总是打印返回的 JSON:看看它,因为 Google 将错误代码和其中的内容存在问题(例如不正确的 API 密钥或者如果您是发送过多的请求等)您是在发送 JSON 数据还是以编码形式发送数据?
-
实际上从那个while循环判断你可能确实发送了太多消息。 GCM 要求您实施指数退避。尝试在 SQL 中添加
LIMIT 1并查看它是否有效。否则请查看返回的 JSON,如上所述。 -
是的,我正在对它们进行编码 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
-
LIMIT 1 只带回一个电话号码,我需要他们全部填写数组并发送到 GCM
-
但是您发送它们的速度可能太快了。它是否有 100% 的时间使用
LIMIT 1发送?更好的方法是使用 JSON 并发送多个 ID(请参阅registration_idsdeveloper.android.com/guide/google/gcm/gcm.html#request)
标签: php android curl google-cloud-messaging