1000 用户 GCM:数组到字符串转换错误

1000 User GCM: Array to String Conversion Error

我正在尝试以 1000 条为单位发送批处理的 GCM 消息。但是,下面的代码 returns 出现了错误 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 消息?

您的代码中没有任何内容表明 GCM 未正确发送;所以我们帮不了你。

但是,这表明您正在尝试回显 updateDataGCM 函数的 return 值,即您已经 return 的数组 $pushStatus;使用 return $pushStatus; 从该函数编辑。

为了知道您正在使用什么,您应该使用 gettype($var),并且始终至少使用 var_dump($var);

旁注

你应该有一个 read about Making the most of Google Cloud Messaging。诸如速率限制和质量控制之类的事情可能会阻碍您发送 1000 个 GCM 的能力(这本身并没有任何意义。)