3

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Smit Patel
  • 1,174
  • 3
  • 26
  • 40
  • 1
    I user parse.com, their basic service is free and you can send up to 1 million pushed every month, check their android sample app and tutorial https://parse.com/apps/quickstart#parse_push/android/new – Pedro Lobito Jan 21 '14 at 16:28
  • 1
    This is for rails, but try this https://github.com/ranjithnfn/PushNotification-ios-android – Ranjithkumar Apr 26 '17 at 12:22

1 Answers1

1

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);

    }

}
Cagatay Gurturk
  • 7,186
  • 3
  • 34
  • 44
  • Thanks but this method is old that we have used now a days google has provided us new method with port 5233 and url for that is http://gcm.googleapis.com .Google has provided the java method also but i want to implement that in php. are you have any other reference? – Smit Patel Jan 22 '14 at 05:21
  • Actually i left this method as well and now i'm using Amazon SNS. If you have to publish notifications for both platforms (APNS and GCM) it's more flexible and deserves its low price. On your server side, you only fire one notification and SNS decides how to send the notification to the device. Look at it. – Cagatay Gurturk Jan 22 '14 at 14:15
  • anyway are you know this?http://stackoverflow.com/questions/19922604/how-to-make-a-unicode-emoji-keyboard-which-convert-iphone-smiley-to-android-like – Smit Patel Jan 23 '14 at 05:10