0

I've seen several posts about how to send GCM messages from my PHP server, but I can't get it working. This is my code:

public function test_gcm($id_user){

    // Search user's RegIds and stores them in $regids

    if(count($regids) == 0){
        echo "This user has no registered device.";
        return;
    }

    $ch = curl_init();

    $data = array(
        'data' => array('message'=>'my message', 'title'=>'message title'),
        'registration_ids' => $regids
    );      

    curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // WRITE JSON HEADERS
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Authorization:key=' . $apiKey)
    );

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $result = curl_exec($ch);

    curl_close($ch);

    echo $result;
}

I'm using the browser key. I tried the server key too, but none of them work, the curl_exec always return false. Does anybody know why is it?

EDIT: I just used 'netstat -tuanc | grep 173' on my server and performed the server call. I'm using grep 173 because if I ping android.googleapis.com I ping this ip address. The netstat didn't show any connection to that ip address when I use the curl_exec. Does that mean I'm not connecting to android.googleapis.com? Or what I'm doing is wrong?

Thanks!

PX Developer
  • 8,065
  • 7
  • 42
  • 66
  • I got it working like this: http://stackoverflow.com/questions/11396177/gcm-sending-with-curl-php Hope it helps – Klaasvaak Sep 20 '12 at 09:30
  • Also: I made it allow calls from all IPs. Test it like that first. I used the browser key aswell. – Klaasvaak Sep 20 '12 at 09:32
  • Yours is one of the posts I checked. I modified the order to have it exactly like you, but it's still not woking. I have "Any IP allowed" for the server key and "Any referer allowed" for the browser key. – PX Developer Sep 20 '12 at 11:10
  • Did you remove the space between Content-type: application/json? I have no idea if this would make any defference. – Klaasvaak Sep 20 '12 at 12:31

4 Answers4

1

Check the "message" content are same or not in android code. 'message'=>'my message' should match with the message from IntentService class in android.

xyz
  • 308
  • 2
  • 3
  • 10
  • I'm using the demo sample provided by Google (http://developer.android.com/guide/google/gcm/demo.html). The function GCMIntentService->onMessage just displays a static string, it doesn't check the message value. – PX Developer Sep 20 '12 at 09:22
  • but as u r running php server there must be some value to interact with your android code. For this your onMessage must have "my message" value – xyz Sep 20 '12 at 09:33
  • I guess you are telling me to check the received message. Well, the first line of the function onMessage is Log.i(TAG, "Received message"); . That log entry isn't shown, so the function onMessage is never called, it seems that the client doesn't receive the message. – PX Developer Sep 20 '12 at 10:21
  • ok. is there any method like this in your onMessage method? : generateNotification(arg0, arg1.getStringExtra("your message tag")); if yes replace "your mesage tag" with "my message" . Hope this will help you. – xyz Sep 20 '12 at 10:27
  • There is a generateNotification method, but the first arg is the context and the second one is a static string ("From GCM: you got message!"). generateNotification(context, getString(R.string.gcm_message)); – PX Developer Sep 20 '12 at 10:31
  • Done. Still the same, the function isn't called. Is there any way to know the error code or something like that? Just returning false is a bit frustrating. – PX Developer Sep 20 '12 at 10:54
0

I've managed to fix it. It was a firewall issue, my firewall was blocking the connection. I've added the rules to accept these messages and now it works.

Thanks to all the people that tried to help :)

PX Developer
  • 8,065
  • 7
  • 42
  • 66
0

Try to change it from https to http

curl_setopt($ch, CURLOPT_URL, 'https://android.googleapis.com/gcm/send');

to

curl_setopt($ch, CURLOPT_URL, 'http://android.googleapis.com/gcm/send');
Jay Gilford
  • 15,141
  • 5
  • 37
  • 56
-1

try this http://2mecode.blogspot.hk/2013/01/google-cloud-messaging-php.html

hope it can help you

2mecode
  • 21
  • 1
  • 3
  • Hi and welcome to SO - please note that link-only answers are generally frowned upon - answers should be complete and use links for references, not for the entire answer content. I recommend editing your answer to put the relevant code (and explanation of how it will help). – Krease Jan 02 '13 at 07:16