Skip to content

Instantly share code, notes, and snippets.

@raviy1290
Created June 13, 2017 15:34
Show Gist options
  • Save raviy1290/6c9b0f46cde8e967af97be08e8586f21 to your computer and use it in GitHub Desktop.
Save raviy1290/6c9b0f46cde8e967af97be08e8586f21 to your computer and use it in GitHub Desktop.
Sending GCM notification easily in PHP
function send_gcm_notification($registatoin_ids, $title=null, $description=null){
$GOOGLE_API_KEY = 'AIzaZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$post_data = array();
$post_data['to']=$registatoin_ids;
$post_data['notification']=array('body'=>'your body', 'title'=>'your title');
$post_data['data']=array('body'=>array('title'=>$title, 'description'=>$description));
$headers = array(
'Authorization: key=' . $GOOGLE_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);
// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
// Execute post
try {
$result = curl_exec($ch);
if ($result === FALSE) {
// log it
//log('Curl failed: ' . curl_error($ch));
}
} catch (Exception $ex){
// log it
}
// Close connection
curl_close($ch);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment