I try push notification to android device with PHP. When i insert a text into text field and send it to android device. It sent success but no content in message on device.Why? And how do i fix it? This is my PHP code:
<?php
include('config/dbconnect_log.php');
if(isset($_POST['submit'])){
function sendPushNotification($registration_ids, $message) {
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registration_ids,
'data' => $message,
);
define('GOOGLE_API_KEY', 'my google api key');
$headers = array(
'Authorization:key=' . GOOGLE_API_KEY,
'Content-Type: application/json'
);
echo json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if($result === false)
die('Curl failed ' . curl_error());
curl_close($ch);
return $result;
}
$pushStatus = '';
$query = "SELECT DISTINCT googleid FROM ggevent";
if($query_run = mysql_query($query)) {
$gcmRegIds = array();
$i = 0;
while($query_row = mysql_fetch_assoc($query_run)) {
$i++;
$gcmRegIds[floor($i/1000)][] = $query_row['googleid'];
}
}
$pushMessage = $_POST['message'];
if(isset($gcmRegIds) && isset($pushMessage)) {
$message = array('msg' => $pushMessage);
$pushStatus = array();
foreach($gcmRegIds as $val) $pushStatus[] = sendPushNotification($val, $message);
}
}
?>
<html>
<head>
</head>
<body>
<article class="module width_full">
<form method = 'POST' action = ''>
<fieldset>
<label>Content</label>
<textarea rows = 2 name = "message" placeholder = 'Messages to Transmit via GCM'></textarea>
</fieldset>
<div class="clear"></div>
</div>
<footer>
<div class="submit_link">
<input type="submit" name='submit' value="Send Push Notification" class="alt_btn">
</div>
</footer>
</form>
</article>
</body>
</html>