Tushar Rmesh Saindane
Tushar Rmesh Saindane

Reputation: 350

IONIC 2: How to add the custom icon and image in the push notification using PHP and firebase

This is my PHP code for sending push notification to ionic app:

$token = json_encode(array("token"=>$token_array)) ; 

$title = "This is Title";
$body = "Test Body Message";

$notification = array(
                   'title' =>$title , 
                   'text' => $body,
                   'vibrate'=>1,
                   "icon"=> "appicon",
                   'sound' => 'mySound'
               );

$arrayToSend = array(
                  'registration_ids'=>$token_array, 
                  'notification' => $notification,
                  'priority'=>'high'
               );

$json = json_encode($arrayToSend);

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key= MYKEY'; 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);       

$response = curl_exec($ch);    
curl_close($ch);

I have included the online link for icon parameter but still, it doesn't work. Please suggest where I am getting wrong.. Thanx in advance.

Upvotes: 1

Views: 700

Answers (1)

masud_moni
masud_moni

Reputation: 1250

You can try following codes which worked for me. It shows icon on most of the devices except Samsung. I am now trying to solve that issue.

$message_for_push = array(
        "data" => array(
            "title" => "New Content Published!",
            "body"  => $message['title'],
            "icon"  => "myicon",
            "color"  => "#e81010",
            'sound' => 'mySound',
        ),

        "registration_ids" =>  $registrationIds ///unique device ids json encoded array
    );
$message_for_push = json_encode($message_for_push);
$curl = curl_init();

curl_setopt_array($curl, array(
        CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_SSL_VERIFYHOST=>0,
        CURLOPT_SSL_VERIFYPEER=>0,
        CURLOPT_POSTFIELDS => $message_for_push,
        CURLOPT_HTTPHEADER => array(
            "authorization: key=AAAArIw35_I:AUTHORIZATION_KEY_HERE",
            "cache-control: no-cache",
            "content-type: application/json"
        ),
    ));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

Hope it helps..:)

Upvotes: 1

Related Questions