Created
April 2, 2014 14:29
-
-
Save stefanhoth/9935325 to your computer and use it in GitHub Desktop.
Example on how to send a message through Google Cloud Messaging (GCM, see http://developer.android.com/google/gcm/server.html ) using a simple PHP script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Replace with real BROWSER API key from Google APIs | |
$apiKey = "<ENTER API KEY HERE>"; | |
// Replace with real client registration IDs | |
$registrationIDs = array( | |
"APA91bGOrAyHktj3DU0H0z4tsu-...", | |
"APA91bHr0vERPIe-DHbQGZLBjuj..." | |
); | |
// Message to be sent | |
$message = "Testing the wonderful world of GCM. Pretty awesome!"; | |
$messageType = "100002"; | |
// Set POST variables | |
$url = 'https://android.googleapis.com/gcm/send'; | |
$fields = array( | |
'registration_ids' => $registrationIDs, | |
'restricted_package_name' => 'com.example.myandroidapp', | |
'collapse_key' => 'somekey_'.$messageType, | |
'data' => array( "KEY_GCM_MESSAGE_TYPE" => $messageType, | |
"payload" => $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 ) ); | |
// Execute post | |
$result = curl_exec($ch); | |
// Close connection | |
curl_close($ch); | |
//echo "Requested: ".json_encode( $fields ); | |
//echo "\n\n<br/><br/>"; | |
echo $result; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment