Question

There is notification in my android app.....but i cant see any notifications on my device... heres my code...

function androidnotify($deviceToken,$message,$app,$badge,$alerttype,$xyzid)
    {
    $apiKey = ".........";
    $url = 'https://android.googleapis.com/gcm/send';
    $regid[]=$deviceToken;
    $registrationIDs = $regid;
    $fields = array('registration_ids'=> $registrationIDs,
                               'data' => $message,
                   'badge'=> $badge,
                           'alerttype'=> $alerttype,
                'xyzid'   => $xyzid);

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

            $ch = curl_init();
            curl_setopt( $ch, CURLOPT_URL, $url );
            curl_setopt( $ch, CURLOPT_POST, true );
            curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
            $result = curl_exec($ch);
    }

I need notification on my device too...what should i do....

Was it helpful?

Solution

you can get demo of GCM from this LINK

it's working with PHP code

here is a code of it

$registatoin_ids must be array of device token(push tokens) $message also must be array

define("GOOGLE_API_KEY","API_KEY");
class GCM {
        //put your code here
        // constructor
        function __construct() {

        }

        /**
         * Sending Push Notification
         */
        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/*."\n\n".json_encode($fields);*/
        }

    }

To send a GCM create object of GCM CLASS

$gcm = new GCM();

and you can send GCM like

$gcm->send_notification(DEVICE_TOKENS_AS_ARRAY, MESSAGE_AS_ARRAY);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top