我的代码中遇到了一个问题。我正在从php向多个用户发送gcm推送通知。我从数据库中获取了reg id。在代码中我使用foreach循环发送通知。但我不确定为什么它只向一个用户发送推送通知而不是向所有用户发送推送通知。如果我删除推送通知代码,它将显示数据库中的所有注册表。
<?php
$host_name = "localhost";
$user_name = "xxxxxxx";
$user_pass = "xxxxx";
$db_name = "xxxxxx";
$conn=mysql_connect($host_name,$user_name,$user_pass);
if(!$conn) {
echo"could not connect:".mysql_error();
} else {
mysql_select_db($db_name,$conn);
}
$query = "select reg_id from property_register where reg_id != ''";
$result = mysql_query($query) or die(mysql_error());
foreach(mysql_fetch_array($result) as $r_id) {
$gcmRegID = $r_id[0];
echo $gcmRegID . "<br>";
$pushMessage = "New Property Posted in CPO";
if (isset($gcmRegID) && isset($pushMessage)) {
$gcmRegIds = array($gcmRegID);
$message = array("m" => $pushMessage);
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $gcmRegIds,
'data' => $message,
);
// Update your Google Cloud Messaging API Key
define("GOOGLE_API_KEY", "xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
}
?>
简而言之,foreach循环只向第一个用户发送通知,而不是全部。如果我删除gcm代码并对foreach循环记录进行回显,则显示所有
由于
答案 0 :(得分:1)
你的foreach循环中有一个返回,这将导致第一次通过foreach来触发retrun调用,并且永远不会遍历另一个foreach。不需要返回。请改用以下代码。
<?php
$host_name = "localhost";
$user_name = "xxxxxxx";
$user_pass = "xxxxx";
$db_name = "xxxxxx";
$conn=mysql_connect($host_name,$user_name,$user_pass);
if(!$conn) {
echo"could not connect:".mysql_error();
} else {
mysql_select_db($db_name,$conn);
}
$query = "select reg_id from property_register where reg_id != ''";
$result = mysql_query($query) or die(mysql_error());
foreach(mysql_fetch_array($result) as $r_id) {
$gcmRegID = $r_id[0];
echo $gcmRegID . "<br>";
$pushMessage = "New Property Posted in CPO";
if (isset($gcmRegID) && isset($pushMessage)) {
$gcmRegIds = array($gcmRegID);
$message = array("m" => $pushMessage);
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $gcmRegIds,
'data' => $message,
);
// Update your Google Cloud Messaging API Key
define("GOOGLE_API_KEY", "xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
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_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
}
}
?>