How to add image to android push notification from firebase. developed using ionic 3

Sample notification

I want to add an image to push notification. am using firebase and app is developed using ionic 3 frameworks. I have a web app to push the notification. this is the PHP code. Please help me to add an image to push notification.

public function pushNotification($title, $body, $image) {
        //FCM API end-point
        $url = 'https://fcm.googleapis.com/fcm/send';
        //api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
        $server_key = 'xxxx';
        
        $target = [];

        $targets = $this->db->get('device_ids')->result();

        foreach($targets as $val) {
            array_push($target, $val->device_id);
        }

        $fields = array();

        $fields['notification'] = array(
            'title' => $title, 
            'body' =>  $body
        );

        
        if(is_array($target)){
            $fields['registration_ids'] = $target;
        }else{
            $fields['to'] = 'news';
        }
        //header with content_type api key
        $headers = array(
            'Content-Type:application/json',
                'Authorization:key='.$server_key
        );
        //CURL request to route notification to FCM connection server (provided by Google)			
        $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_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Oops! FCM Send Error: ' . curl_error($ch));
        }
        curl_close($ch);
        print_r(json_encode($fields));
    }

Upvotes: 0

Views: 6594

Answers (2)

user9006594
user9006594

Reputation:

This is for IONIC 3 notification with Image and Text from firebase Implement Phonegap Push Plugin and create your firebase project ionic cordova plugin add phonegap-plugin-push //Now we send the Notification Post service


URL=https://fcm.googleapis.com/fcm/send
Header: Authorization: key={Your Firebase Key}
        Content-Type:  application/json

then send Body Body:

***

{
      "to": "/topics/college",
      "data": {
        "title": "Notification With Image",
        "message": "This is my Notification with Image",
        "style": "picture",
        "picture": https://appsolzone.com/wp-content/uploads/2018/07/Scholar-Wings-2-480x336.jpg",
        "summaryText": "This is image notification with Firebase in IONIC"
      }
    }

Upvotes: 0

Parvej Mallick
Parvej Mallick

Reputation: 465

$tokenListAndroid = "YOUR_TOKEN";

    define( 'API_ACCESS_KEY', 'YOUR_KEY');
     $Message='YOUR_MESSAGE';
     $offerId=$_POST['offerId'];
    $iOSids = explode(",",$tokenListiOS);
    $androidIds = explode(",",$tokenListAndroid);


    $msg = array
    (
        'title'         => 'TEXT',
        'message'      => 'TEXT',
        'summaryText'      => 'TEXT',
        'orderId'       => 'TEXT',
        'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
        'image' => 'YOUR_ICON_PATH ex. https://carboncostume.com/wordpress/wp-content/uploads/2016/08/blackpanther.jpg',
        'style' => 'picture',
        'picture' => 'YOUR_IMAGE_PATH ex. https://carboncostume.com/wordpress/wp-content/uploads/2016/08/blackpanther.jpg',
        //'ongoing' => true,
        'vibrate'   => 1,
        'sound'     => 1
    );

    $fields = array
    (
        'registration_ids'  => $androidIds,
        'data'              => $msg
    );

    //echo json_encode( $fields ); die; 
    $headers = array
    (
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
    );

Thats all XD

Upvotes: 1

Related Questions