无法使用GCM发送PUSH通知

时间:2013-04-06 19:45:43

标签: java php proxy google-api google-cloud-messaging

我的本​​地主机上有一些正常运行的代码,但当我将它全部移动到我的服务器时,它似乎无法正常工作。

我的服务器在防火墙后面,所以我决定使用PHP脚本来使用GCM。再一次,我在localhost上对它进行了全部测试,但是在我的服务器上运行时,没有任何内容被发送。

这是我的PHP脚本:

<?php
include('tools.php');
// Replace with real BROWSER API key from Google APIs
$apiKey = "xxxx";
// Replace with real client registration IDs 
//$registrationIDs = array($_POST['devices']);
$registrationIDs = $_POST['devices'];
$proxy = 'http://proxy.vmsrv.redbrick.dcu.ie:3128';

// Message to be sent
$message = $_POST['message'];

// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
                'registration_ids'  => $registrationIDs,
                'data'              => array( "message" => $message ),
                );

$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_PROXY, $proxy);
// Execute post
$result = curl_exec($ch);

// Close connection
curl_close($ch);

print_as_json($result);

// Report all PHP errors
error_reporting(-1);
?>

我的Java程序调用脚本后,我在控制台中得到的唯一输出是:

[java] DB: Result: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given in /var/www/tools.php on line 5[]

无论如何,我可以尝试获取一些信息,以找出问题是什么???

修改 tools.php

<?php

function print_as_json($result) {
    $all = array();
    while($r = mysql_fetch_assoc($result)) {
        $all[] = $r;
    }
    echo(json_encode($all));
}


?>

1 个答案:

答案 0 :(得分:1)

删除行

print_as_json($result);

而是使用简单的回声。

echo $result;

或者您可能想要尝试查看它是什么类型的对象:

var_dump($result);

我不确定$结果是什么样的。如果它是json字符串,您可以将其转换为数组:

$array = json_decode($result);
相关问题