-1

download GCMDemo from http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

Registering with Google Cloud Messaging https://code.google.com/apis/console/b/0/?noredirect&pli=1#project:243064666795:access

Change ip with my local system static final String SERVER_URL = "http://142.168.1.213/gcm_server_php/register.php";

Change sender id like static final String SENDER_ID = "243064666795";

Change Google key, username, password, dbname in config.php

After then start project on emulator it's successfully register on device on server it display one device on browser also but when i send notification from browser it will not receive on device. what's wrong ?

Cœur
  • 32,421
  • 21
  • 173
  • 232
PankajAndroid
  • 2,593
  • 3
  • 23
  • 41

1 Answers1

0
        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 ;
Kushal Suthar
  • 425
  • 6
  • 20