Monday, December 5, 2016

Firebase Cloud Messaging (FCM) PUSH Notification Documentation (Android)

Example:1

<?php // Dynamic
    date_default_timezone_set("Asia/Kolkata");
    function save_and_send_push_notification(){

        define('FIREBASE_API_KEY', 'AIzaSyDf-uPyTgxcxckH2JLE6lO0XmygxWmjFpS7BU0'); // this api key created from fcm account panel
                $title = trim($this->input->post('title'));
                $message = trim($this->input->post('message'));
                $type = trim($this->input->post('type')); // 'All', 'Multiple'
                $user_ids = trim($this->input->post('user_ids')); // this is an array type


                    $payload = array();
                    $payload['team'] = 'India';
                    $payload['score'] = '5.6';
                    $global = 'global';
                    $to = '/topics/' . $global;
                   
                    $res = array();
                    $res['data']['title']           = $title;
                    $res['data']['is_background']   = false;
                    $res['data']['message']         = $message;
                    $res['data']['image']           = 'http://images.carandbike.com/car-images/big/bmw/1-series/bmw-1-series.jpg';
                    $res['data']['payload']         = $payload;
                    $res['data']['timestamp']       = date('Y-m-d H:i:s');

                if($type == 'All'){
                        $global = 'global'; // Topic name provided by android developer
                        $to = '/topics/' . $global;
                        $fields = array('to'=>$to,'data'=>$res);

                }else{
                          $to = array("ffe3mf5m454","df68dfrbb5","fgfhf9fg9gfg"); // device tokens
                          $fields = array('registration_ids'=>$to,'data'=>$res);

                }

        $url = 'https://fcm.googleapis.com/fcm/send';
        $headers = array(
                        'Authorization: key=' .FIREBASE_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_PROXY, '10.166.105.54:3128'); if curl not working in live server add this line
        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
        echo $result;
        curl_close($ch);
        // finally if the notification sent. insert these information into one table like=> title,message,image,type and user_ids
}
?>
Example:2
    <?php  Static:
    date_default_timezone_set("Asia/Kolkata");
    // old start
      //define('FIREBASE_API_KEY', 'AIzaSyDf-uPxxcxyTgkH2JccczxLE6lO0XmygxWmjFpS7BU0');
      // $to = 'f7paTxPtMNI:APA91bG5FsRdwdB-W-CmKUDg-5mRgqVEvZ4XEycpel4my0ttR6jEw4ZvtqKD_jROxxcxcz3vkKqV-JPr1ablXjHowY9bQOyAP-sM_kK_zpVdbJuQ-bfO4hG9QyFWjf_193xZuxcxc5IZDtChU6SiF';
   
        // new start
        define('FIREBASE_API_KEY', 'AIzaSyB0J97UiXxcxzcuNNsf9fLRxcHGu4WQ3_Oam3wFgI');
       $to = 'eIE-xNAybZU:APA91bGkrLxceE1mFPfkN9vkBtTuQgmE3LASXAN47HDwCkZfiyFziJ1Ae8GYV8FpRXjsYcT5-vC5AGF0pbxavhIL_j8gxyheKlBqnRxxzcX3CSHD0gvkvfzSH2xcxzcxR8iJDYwGb2ZBVjoeCPLfRs8R';
        $payload = array();
        $payload['team'] = 'India';
        $payload['score'] = '5.6';
            $global = 'global';
      // $to = '/topics/' . $global;
       
        $res = array();
        $res['data']['title']           = 'IBB Trade Offers';
        $res['data']['is_background']   = false;
        $res['data']['message']         = '25 % offers on 1 year used cars';
        $res['data']['image']           = 'http://images.carandbike.com/car-images/big/bmw/1-series/bmw-1-series.jpg';
        $res['data']['payload']         = $payload;
        $res['data']['timestamp']       = date('Y-m-d H:i:s');
                $fields = array(
                        'to'    => $to,
                        'data'  => $res,
                        );

        $url = 'https://fcm.googleapis.com/fcm/send';
        $headers = array(
                        'Authorization: key=' .FIREBASE_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_PROXY, '10.166.105.54:3128'); if curl not working in live server add this line
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        //Execute post
        $result = curl_exec($ch);
        if ($result === FALSE)
            die('Curl failed: ' .curl_error($ch));

       //var_dump($result);die;
        //Close connection
        //echo "<pre>"; print_r($fields);
        echo "<pre>"; print_r(json_decode($result));
        echo $result;
        curl_close($ch);
        return $result;
?>

No comments:

Post a Comment