I'm trying to use php class to send data to app(phonegap) using GCM. Here the data is stored to the database and it is send using the Php-GCM class. The problem is that it is showing null values when sending for all columns.
<?php
class GCMPushMessage {
var $url = 'https://android.googleapis.com/gcm/send';
var $serverApiKey = "xxxxxxx";
var $devices = 0;
function setDevices($deviceIds)
{
if(is_array($deviceIds)){
$this->devices = $deviceIds;
} else {
$this->devices = array($deviceIds);
}
}
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;
}
}
$headers = array(
'Authorization: key=' . $this->serverApiKey,
'Content-Type: application/json'
);
// 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 ) );
// Avoids problem with https certificate
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
return $result;
}
}
$id=$_POST['id'];
$diagnosis=$_POST['diagnosis'];
$instructions=$_POST['instructions'];
$doc_name=$_POST['doc_name'];
$med_id=time().rand(11,99).time();
$str="insert into prescription values('$id','$diagnosis','$instructions','$doc_name','$med_id')";
$res=@mysql_query($str)or die(mysql_error());
$nf=$_POST['nf'];
$i=1;
while($i<=$nf)
{
$medicine=' ';
$tm1=$tm2=$tm3=0;
$medicine=$_POST['medicine_'.$i];
$tm='';
if(isset($_POST['tm_1_'.$i]))
{$tm1=1;}
if(isset($_POST['tm_2_'.$i]))
{$tm2=1;}
if(isset($_POST['tm_3_'.$i]))
{$tm3=1;}
$dosage=$_POST['dosage_'.$i];
$str="insert into medicine values('$med_id','$dosage','$medicine','$tm1','$tm2','$tm3')";
$res=@mysql_query($str)or die(mysql_error());
$i++;
}
$id = $_POST['id'];
$gcpm = new GCMPushMessage();
$sql=mysql_query("select token from device where id=".$id);
$rs=mysql_fetch_assoc($sql);
$gcpm->setDevices($rs['token']);
$query1=mysql_query("select * from medicine,prescription where med_id=mid and id=".$id);
while($rs1=mysql_fetch_assoc($query1))
{
$rows[]['medicine_name']=$rs['medicine_name'];
$rows[]['tm_1']=$rs['tm_1'];
$rows[]['tm_2']=$rs['tm_2'];
$rows[]['tm_3']=$rs['tm_3'];
$rows[]['dosage']=$rs['dosage'];
}
$rows[]['diagnosis']=$rs['diagnosis'];
$rows[]['instructions']=$rs['instructions'];
print_r($rows);
$response = $gcpm->send($message, $rows);
?>
When I try to display $rows,it is showing null value for all the items. But the data is getting inserted into the db. Sorry for posting the whole code. I'm a newbie. Please help.
It is difficult to figure out the root cause of the issue since you have not posted the log cat. The exact cause of the error may be a NULL value but at which line it is occuring would help more to diagnose the issue.
I would say you to have a look at this tutorial that deals with inserting and fetching up off multiple rows from two tables.
For GCM implementation please have a look at this tutorial. And make sure the structure is modular as described in the example.
Hope that Helps!!