Tuesday 14 October 2014

PUSH NOTIFICATION FOR ANDROID ( PHP )

for push notification you have to use only this code.. not more programing is required...

// this is the google apis url.. not need to change..

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

//$fieds is an array and $registatoin_ids is a device id for which you have to create push notification

//if you create push notification for multiple device then use $registatoin_ids  as

// device id is a device token which is unique for every device/cellphone

//$registatoin_ids = array(deviceid 1, device id 2, device id 3,.....);

$message = array("You have receiving a push notification");

//$message is a text which you want to send.. in message

        $fields = array(
            'registration_ids' => $registatoin_ids,
            'data' => $message,
        );

//GOOGLE_API_KEY use here google server key which is made by android developer..

// we use GOOGLE BROWSER Key

        $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;
// if your code is fine then result give you the response and success 1 if you got error then result give you error  message.. or failure 1

No comments:

Post a Comment