简体   繁体   中英

Can't send PUSH notifications with GCM

I have some code working fine on my localhost, but when I move it all to my server, it doesn't seem to work.

My server is behind a firewall so I decided to use a PHP script to use GCM. Again, I tested it all on localhost and it works but on my server, nothing gets sent.

Here is my PHP script:

<?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);
?>

The only output I get in my console after my Java program has called the script is:

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

Is there anyway I can try get some information back to figure out what the problem is???

Edit tools.php

<?php

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


?>

Remove the line

print_as_json($result);

And use a simple echo instead.

echo $result;

Or you might want to try to see what kind of object it is:

var_dump($result);

I'm not sure what $result looks like. If it is a json string, you can convert it into an array with:

$array = json_decode($result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM