Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
PHP
$var = select * from table;

foreach($var as $varliable)
{
   $to_device_id = $varliable['id']
   $message = array("unread"=>$msg);  // storing multiple messages
   $device_id = array($to_device_id); // storing multiple device ids

   $gcmObj->send_notification($device_id, $message); // this function is called in another page test.php
}


test.php
========
PHP
function send_notification($device_id, $message="")
{
   $curl = 'https://android.googleapis.com/gcm/send';

   // how to replace the device id and messages here. 

   $field= array(
      'registration_ids' => $device_id,
      'field' => $message,
      );
   $headers = array(
      'Authorization: key=' . GOOGLE_API_KEY,
      'Content-Type: application/json'
      );
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $curl);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($field));
   $result = curl_exec($ch);      
   curl_close($ch);
   echo $result;
}
Posted
Updated 12-May-14 18:55pm
v4

1 solution

Try this modification:
PHP
$fields = array(
'registration_ids' => $device_id,
'data' => $message
);

// other code

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900