Skip to content

Instantly share code, notes, and snippets.

@long-nguyen
Created December 16, 2013 07:50
Show Gist options
  • Save long-nguyen/7983624 to your computer and use it in GitHub Desktop.
Save long-nguyen/7983624 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<body>
<?php
/**
* The following function will send a GCM notification using curl.
*
* @param $apiKey [string] The Browser API key string for your GCM account
* @param $registrationIdsArray [array] An array of registration ids to send this notification to
* @param $messageData [array] An named array of data to send as the notification payload
*/
function sendNotification( $apiKey, $registrationIdsArray, $messageData )
{
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$data = array(
'data' => $messageData,
'registration_ids' => $registrationIdsArray
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// Message to send
$message = "I am Long, I am sending test message 3";
$tickerText = "ticker text message";
$contentTitle = "content title";
$contentText = "content body";
$registrationId = 'APA91bE5C1RVeO26EsaJg0obPFmYRvCPzar7gPX2fN2OjJ7-G8RGyUPyuHPilOjqopn_cRsm-qY6YWha-NwLk4zmB6wT1-y7NDzY4TSOtAj_a-BV_cH2ZlpH0ir-KeVIEof0B4c7OjEVqSfKXmUPxOEo0mUgMLZKpn_c_bpKsNMrQjRK8lvjw5snpCChY-vtt6TfbH8rjZhZ';
$apiKey = "AIzaSyCLCuwwpnQZKImHNBYZwzywFkRzvaIPEEs";
$response = sendNotification(
$apiKey,
array($registrationId),
array('message' => $message, 'tickerText' => $tickerText, 'contentTitle' => $contentTitle, "contentText" => $contentText) );
echo $response;
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment