Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch 1 #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions GCMPushMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@
http://stackoverflow.com/questions/11242743/gcm-with-php-google-cloud-messaging

*/

class GCMPushMessage {

var $url = 'https://android.googleapis.com/gcm/send';
var $serverApiKey = "";
var $devices = array();

/*
Constructor
@param $apiKeyIn the server API key
*/
function GCMPushMessage($apiKeyIn){
*/

public function __construct($apiKeyIn){
$this->serverApiKey = $apiKeyIn;
}

Expand All @@ -38,13 +40,13 @@ function GCMPushMessage($apiKeyIn){
@param $deviceIds array of device tokens to send to
*/
function setDevices($deviceIds){

if(is_array($deviceIds)){
$this->devices = $deviceIds;
} else {
$this->devices = array($deviceIds);
}

}

/*
Expand All @@ -53,20 +55,20 @@ function setDevices($deviceIds){
@param $data Array of data to accompany the message
*/
function send($message, $data = false){

if(!is_array($this->devices) || count($this->devices) == 0){
$this->error("No devices set");
}

if(strlen($this->serverApiKey) < 8){
$this->error("Server API Key not set");
}

$fields = array(
'registration_ids' => $this->devices,
'data' => array( "message" => $message ),
);

if(is_array($data)){
foreach ($data as $key => $value) {
$fields['data'][$key] = $value;
Expand All @@ -80,25 +82,25 @@ function send($message, $data = false){

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $this->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);

return $result;
}

function error($msg){
echo "Android send notification failed with error:";
echo "\t" . $msg;
Expand Down