Question

I have implemented push notification using GCM. Everything is working perfect when I run necessary files to send messages to device from my local server (on my computer with XAMPP server). But when I upload files to my live server and when I run the files, I'm not getting push notification. I verified all the files but I'm not getting. Below is my php code:

send_message.php

<?php

if (isset($_REQUEST["regId"]) && isset($_REQUEST["message"]) && isset($_REQUEST["senderid"])) {
    $regId = $_REQUEST["regId"];
    $message = $_REQUEST["message"];
    $senderid = $_REQUEST["senderid"];


    include_once './GCM.php';

    $gcm = new GCM();

    $registatoin_ids = array($regId);

    $message = array("message" => $message, "sender_id" => $senderid);
    $result = $gcm->send_notification($registatoin_ids, $message);
    echo $result;die;

}

?>

GCM.php

<?php

    class GCM {

        function __construct() {

        }

        public function send_notification($registatoin_ids, $message) {
            // include config
            include_once './config.php';


            // Set POST variables
            $url = 'https://android.googleapis.com/gcm/send';

            $fields = array(

                'registration_ids' => $registatoin_ids,
                'data' => $message, 

            );

            $headers = array(
                'Authorization: key=' . GOOGLE_API_KEY,
                'Content-Type: application/json'
            );

            // Open connection
            $ch = curl_init();


            // Set the url, number of POST vars, POST data
            curl_setopt($ch, CURLOPT_URL, $url);

            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            // Disabling SSL Certificate support temporarly
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

            // Execute post
            $result = curl_exec($ch);

            if ($result === FALSE) {
                die('Curl failed: ' . curl_error($ch));
            }

            // Close connection
            curl_close($ch);
            echo 'result';
            echo json_encode($fields);
            echo $result;
        }

    }

    ?>

I'm executing send_message.php file from my browser by providing server url and necesasry parameters. On local machine, I'm getting push notification, but while I executing the same file by providing live server url, it is not giving me any output. What can be the issue?

Was it helpful?

Solution

When you want to access PHP page from live address you just need to enable cURL from your XAMPP

Go to your php.ini file and un-comment following line ( delete ; will remove it from comment ),

;extension=php_curl.dll

After doing this you can check curl in phpinfo() like below image,

enter image description here

For LAMP

From http://buzznol.blogspot.com/2008/12/install-curl-extension-for-php-in.html:

sudo apt-get install curl libcurl3 libcurl3-dev php5-curl

After installing libcurl you should restart the web server with one of the following commands,

sudo /etc/init.d/apache2 restart OR sudo service apache2 restart

OTHER TIPS

The problem might be of the API Key you are using to send push notifications to the Android Device.

Two kinds of API keys are available in the Google Developers Console:

Use a server key if your application is running on a server.

Use a browser key if your application is running on a client, for example from JavaScript or any other client-side web-browser context.

Below is the screenshot of the Developers Console, where you would get the browser & server API Keys.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top