Advertisement
Guest User

Post GCM using PHP

a guest
Feb 4th, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.36 KB | None | 0 0
  1. <?php
  2. // Replace with real BROWSER API key from Google APIs
  3. $apiKey = "your-api-key";
  4.  
  5. // Replace with real client registration IDs. Put the correct registration id you are getting from app. If you are using eclipse check your logcat you will get it there.
  6. $registrationIDs = array( "regid" );
  7.  
  8. // Message to be sent
  9. $message = "Your Message Here";
  10.  
  11. // Set POST variables.
  12. $url = 'https://android.googleapis.com/gcm/send';
  13.  
  14. $fields = array(
  15.                 'registration_ids'  => $registrationIDs,
  16.                 'data'              => array( "message" => $message ) /*Make sure that message is the key you are using in GCMIntentService.java onMessage() ->  extras.getString("message");*/
  17.                 );
  18.  
  19. $headers = array(
  20.                     'Authorization: key=' . $apiKey,
  21.                     'Content-Type: application/json'
  22.                 );
  23.  
  24. // Open connection
  25. $ch = curl_init();
  26.  
  27. // Set the url, number of POST vars, POST data
  28. curl_setopt( $ch, CURLOPT_URL, $url );
  29.  
  30. curl_setopt( $ch, CURLOPT_POST, true );
  31. curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
  32. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  33. curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  34. curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
  35.  
  36. // Execute post
  37. $result = curl_exec($ch);
  38.  
  39. // Close connection
  40. curl_close($ch);
  41.  
  42. // Echo success or failure
  43. echo $result;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement