Как добавить изображение в push-уведомление Android от firebase.разработан с использованием ионных 3 - PullRequest
0 голосов
/ 01 июня 2018

Sample notification

Я хочу добавить изображение для push-уведомления.Я использую Firebase и приложение разработано с использованием ионных 3 фреймворков.У меня есть веб-приложение для отправки уведомлений.это код PHP.Пожалуйста, помогите мне добавить изображение для push-уведомления.

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

1 Ответ

0 голосов
/ 01 июня 2018
$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'
    );

Вот и все XD

...