Skip to content

Instantly share code, notes, and snippets.

@mohamnag
Created January 20, 2014 22:10
Show Gist options
  • Save mohamnag/1f594d9e1119605fa409 to your computer and use it in GitHub Desktop.
Save mohamnag/1f594d9e1119605fa409 to your computer and use it in GitHub Desktop.
This function sends GCM push messages using cakephp's HttpSocket
public function sendPush($token, $collapseKey, $title, $messageText) {
if(!is_array($token)) {
$token = array($token);
}
App::uses('HttpSocket', 'Network/Http');
$HttpSocket = new HttpSocket();
$data = array(
'registration_ids' => $token,
'collapse_key' => $collapseKey,
'data' => array(
'title' => $title,
'message' => $messageText
)
);
$result = $HttpSocket->post(
'https://android.googleapis.com/gcm/send',
json_encode($data),
array(
'header' => array(
'Authorization' => 'key='.Configure::read('GCM.AuthKey'),
'Content-Type' => 'application/json'
)
)
);
if($result->code !== "200") {
return false;
}
else {
return json_decode($result->body, TRUE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment