1
votes

I am trying to set up Google Cloud Messaging on Android with PHP (following this tutorial: http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/). My device registers successfully with GCM and then with the server (the regId is stored into the database).

When I try and send to the device nothing is received, although I get this response:

{"multicast_id":4701392840778619841,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1355907831651938%978fee92f9fd7ecd"}]}

Google API reports that no requests have been made.

This is my send_notification function in PHP:

    public function send_notification($registration_ids, $message) {
    // include config
    include_once './config.php';

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

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

    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        '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);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    // Execute post
    $result = curl_exec($ch);
    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    // Close connection
    curl_close($ch);
    echo $result;
}

From research, I have seen similar questions have been asked to this, however they vary slightly - for example, different programming languages and no solution that I have found has solved this problem.

Extra Info:

1. I have checked and cURL is enabled.

2. My onMessage method

protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("price");

    displayMessage(context, message);
    // notifies user
    generateNotification(context, message);
}

(price in the above just retrieves the message as its stored like this: $message = array("price" => $message); before being given as a parameter to the send_notification function on the server.

I have checked LogCat and 'Received message' does not appear.

3
Are you sure that your API key is correct, and that your server IP is listed as trusted by the certificate? The data property cannot be a multidimensional array. You need to define them on the top level as such: data.price data.messageDaniel
Thanks for your response Daniel, I have checked the API key is correct and currently it allows any IP. I have tried using 'data.price' => "test" and also 'data' => "test", but still no luck :(user1804590
I also got this problem... please refer to this answer stackoverflow.com/a/12349953 it then works like a charm the GCM message comes to my real device...user1325609

3 Answers

2
votes

Just solved this (although I don't know what was causing the original problem). I shut my device down and started it up again and it went mad receiving all the messages I had sent it over the last few days. Now it receives them as they are sent, so assuming everything else is correct, if anyone is having this problem, try restarting the device.

0
votes

please generate and use server api key to view for live notification on device.

that is not mention in this post

http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

0
votes

I experienced a very similar (same?) problem. My issue was that I changed the String from "price" in the PHP code, but I hadn't done in the Android code. You need to do both. Maybe this will help someone. :)