define('PUSHURL', 'https://go.urbanairship.com/api/push/');
define('GCMAPIKEY','xxxxxxxxxxxxxxxxxxxxxxxxxxx');
define('GCMPUSHURL', 'https://android.googleapis.com/gcm/send');
$ori_msg=substr($message,0,200);
$querydevices="selectdevice_token from device_info where device_type='A'";
$devicesres = mysql_query($querydevices) or die(mysql_error());
$devicecount = mysql_num_rows($devicesres);
if($devicecount > 0)
{
while($fetchdevices = mysql_fetch_array($devicesres))
{
if($fetchdevices['device_token']!="0")
{
if($fetchdevices['device_token']!="")
{
$registrationIDs[]=$fetchdevices['device_token'];
}
}
}
$fields = array(
'registration_ids' => $registrationIDs, //(android device ids[array()])
'data' => array( "message" => $ori_msg )
);
$payload = json_encode($fields);
// echo $payload;
$headers = array(
'Authorization: key='.GCMAPIKEY,
'Content-Type: application/json'
);
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, GCMPUSHURL);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
// Execute post
$result = curl_exec($ch);
curl_close($ch);
}
EDIT
and this is table structure
CREATE TABLE `device_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`device_token` varchar(500) NOT NULL,
`createdon` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;