我试图以1000块为单位发送批量GCM消息。但是,下面的代码返回错误
Array to string conversion in C:\xampp\htdocs\index.php on line 651
行动中的echo updateDataGCM($db);
:
case "sendGroupMessage":
if ($userId = authenticateUser($db, $username, $password, $gcmregid))
{
if (isset($_REQUEST['toGroupId']))
{ // instead of toUserName it's to a groupId
$toGroupName = $_REQUEST['toGroupName'];
$toGroupId = $_REQUEST['toGroupId'];
$message = $_REQUEST['messageText'];
$campaign = $_REQUEST['campaign_id'];
$location = $_REQUEST['location_id'];
// Query to get the users id's who are in the group but not the user sending the message
$sqlGroupMembers = "SELECT DISTINCT usersId from users_groups
WHERE usersId != '".$userId."' AND groupId = '".$toGroupId."'";
// Loop to create a copy of message for all users taht are part of that group
if($getGroupMembersId = $db->query($sqlGroupMembers))
{
while($rowGroupMembers = $db -> fetchObject($getGroupMembersId))
{
$sql22 = "INSERT INTO `group_messages`...";
error_log("$sql22", 3 , "error_log");
if ($db->query($sql22))
{
$out = SUCCESSFUL;
}
else
{
$out = FAILED;
}
}
}
// Send GCM to turn on devices:
echo updateDataGCM($db);
}
else
{
$out = FAILED;
}
}
else
{
$out = FAILED;
}
break;
updateDataGCM()函数:
function updateDataGCM($db) {
// Get all the GCM regIDs for anyone with new, "unseen" data:
$sqlAllRegIds = 'select distinct gcm_registration_id
from users';
// Execute
if ($resultIds = $db->query($sqlAllRegIds))
{
$gcmRegIds = array();
$i = 0;
while($query_row = $db -> fetchObject($resultIds)) {
$i++;
$gcmRegIds[floor($i/1000)][] = $query_row;
}
$pushMessage = $_POST["syncNewData"];
$message = array("syncNewData" => $pushMessage);
$pushStatus = array();
foreach($gcmRegIds as $val) $pushStatus[] = sendPushNotificationToGCM($val, $message);
return $pushStatus;
}
}
如何才能正确发送GCM消息?
答案 0 :(得分:0)
代码中没有任何内容表明GCM发送不正确;所以我们无法帮助你。
但是,它表示您正在尝试回显updateDataGCM
函数的返回值,该函数是您使用$pushStatus;
从该函数返回的数组return $pushStatus;
。
为了了解您的工作内容,您应该使用gettype($var)
,并且始终使用var_dump($var);
。
<强>旁注强>
你应该有read about Making the most of Google Cloud Messaging。有一些诸如速率限制和质量控制之类的东西,可能会阻碍你发送1000个GCM的能力(这本身并没有任何意义。)