简体   繁体   中英

Android GCM Push Notification server side in java

I have a couple of questions regarding GCM.

  1. On the official page for GCM , under Getting Started, it says Decide which Google-provided GCM connection server you want to use— HTTP or XMPP (CCS). Now I understand the difference between the two of these in practice, but when he says DECIDE which one to choose, I am not sure what is he expecting of me? Is there something I need to do differently for the two of these.
  2. I have the android app ready for GCM. For the server part of things, I am not sure what am I supposed to do. Can I just do a POST request to https://android.googleapis.com/gcm/send nd pass parameters along? I can use HTTPClient for this one, right? Is there something special?
  3. Also, the server that I have to do in step 2. It need not be a server right? It can be any standalone java code and I can just run it, to make a POST to gcm? Only for testing purposes.

2 - that's basically it, here' my server code, it's a PHP file in my server

public function send_push(){
    $message = "your message";

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

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

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

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

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

    // Close connection
    curl_close($ch);

    echo $result;
}

GCM_REGISTRATION is the hash you get after you run your app for the first time to register in GCM. if everything is ok, You can see this in logcat

api_key is the hash you get when you create your app( you have probably seen it on the documentation)

3 - I believe it doesn't need to be a server because it's actually going to send the message to Google servers which is going to deliver it to your app

  1. Yes, there's a big difference between http and ccs severs. The former involves simple http requests. The latter requires xmpp protocol implementation.

  2. Yes, any code that submits http requests should work if you are taking the gcm http approach.

  3. For testing purposes you don't need a server. Any code submitting http requests would do. However, you'll have to pass the device registration id to your server code in order to send a GCM message.

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