doucitao2944 2014-02-15 16:30
浏览 97

从mysql获取数组并将其传递给方法

Hi I am sending the push notification using below code. I've a problem in this code.

<?php

class GCMPushMessage
{

    var $url = 'http://android.googleapis.com/gcm/send';
    var $serverApiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
    var $devices = array();

    function setDevices($deviceIds)
    {

        if (is_array($deviceIds)) {
            $this->devices = $deviceIds;
        } else {
            $this->devices = array(
                $deviceIds
            );
        }
    }

    function send($message)
    {

        if (!is_array($this->devices) || count($this->devices) == 0) {
            $this->error("No devices set");
        }

        if (strlen($this->serverApiKey) < 8) {
            $this->error("Server API Key not set");
        }

        $fields = array(
            'registration_ids' => $this->devices,
            'data' => array(
                "msg" => $message
            )
        );

        $headers = array(
            'Authorization: key=' . $this->serverApiKey,
            'Content-Type: application/json'
        );

        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $this->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));

        // Execute post
        $result = curl_exec($ch);

        $this->StripResponseFromGCM(json_decode($result));

        // Close connection
        curl_close($ch);

        return $result;
    }

    function error($msg)
    {
        echo "Android send notification failed with error:";
        echo "\t" . $msg;
        exit(1);
    }

    function StripResponseFromGCM($response)
    {
        //canonicalID's are the 
        if ($response->failure == 0 && $response->canonical_ids == 0)
            return;

        for ($i = 0; $i < sizeof($response->results); $i++) {
            if (isset($response->results[$i]->registration_id)) { //if new registrationID is sent as canonicalID
                //update this registrationID in the database
            } else if ($response->results[$i]->error == "Unavailable") {
                // user with index == $i is unavailable
            } else if ($response->results[$i]->error == "InvalidRegistration") {
                // user with index == $i has InvalidRegistration ID
            } else if ($response->results[$i]->error == "NotRegistered") {
                // user with index == $i is not registered              
            }
        }
    }

}

$msg = array(
    'data' => array(
        'msg' => 'just a simple message'
    )
);

require_once('connection.php');

$sql = mysql_query("select gcmid from gcmid");

$new_array = mysql_fetch_array($sql);

print_r($new_array);

$obj = new GCMPushMessage();

$obj->setDevices($new_array);

$obj->send($msg);
?>

If i put a single id in setDevices method then it is working fine but if i fetching the gcm ids from database and passing it to method setDevices then the code is not working.It suppose to send push notification to devices but it is not working.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值