How to set up the New Google Cloud Messaging API server implementation and Hosting using PHP?

672 views Asked by At

I am using the new Google Cloud messaging functionality and it's developed successfully in client side and receiving push notification without any dropping. But I'm using an old Send function on the server. Now I want to implement new send function (XMPP) using PHP.

I have registered here also https://services.google.com/fb/forms/gcm/ and got the response mail and key from the google.

And from that I got that I have to implement the SmackCcsClient Java class and two libraries. But I have no idea how to host that file to my PHP server.

After some research I got the function for PHP and xmphp libraries for PHP

$conn = new XMPPHP_XMPP($host, $port, $user, $password, $resource, $server, $printlog, $loglevel);

But can't get the success it's saying could not connect.

1

There are 1 answers

3
Cagatay Gurturk On BEST ANSWER

You can use this class:

class gcm {


    public static function send_notification($deviceid, $message) {


        // include config


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

        $fields = array(
            'registration_ids' => (is_array($deviceid) ? $deviceid : array($deviceid)),
            'data' => array("message" =>$message),
        );

        $headers = array(
            'Authorization: key=YOUR-AUTH-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);

    }

}