How to add image on push notification using brozot / Laravel-FCM

Viewed 2305

How to add image on push notification using brozot / Laravel-FCM ?

I'm sending notifications correctly, but I would like to know how can I send an image with the notification?

I tried this code but not working

        $pushData = ['body' => $message, 'title'=>$title,'image'=>'image-url'];




    $pushJsonData = json_encode($pushData);
    if(count($tokens)>0)
    {


        $optionBuilder = new OptionsBuilder();
        $optionBuilder->setTimeToLive(60*20);

        $notificationBuilder = new PayloadNotificationBuilder($title);
        $notificationBuilder->setClickAction('NOTIFICATION');
        $notificationBuilder->setBody($message)->setSound('default');
        $notificationBuilder->setTag(strtotime("now"));


        $dataBuilder = new PayloadDataBuilder();
        $dataBuilder->addData(['a_data' => $pushJsonData]);

        $option = $optionBuilder->build();
        $notification = $notificationBuilder->build();
        $data = $dataBuilder->build();


        $downstreamResponse = FCM::sendTo($tokens, $option, $notification, $data);

        $downstreamResponse->numberSuccess();
        $downstreamResponse->numberFailure();
        $downstreamResponse->numberModification();

        //return Array - you must remove all this tokens in your database
        $downstreamResponse->tokensToDelete();

        //return Array (key : oldToken, value : new token - you must change the token in your database )
        $downstreamResponse->tokensToModify();

        //return Array - you should try to resend the message to the tokens in the array
        $downstreamResponse->tokensToRetry();

        // return Array (key:token, value:errror) - in production you should remove from your database the tokens present in this array
        $downstreamResponse->tokensWithError();
2 Answers

You need to create a custom script that herance the vendor script and add some properties on it.

  • Create a new path in app: app/Notifications/Message

  • Add a new script called CustomPayloadNotification.php

Here you need to:

  1. Extends PayloadNotification (vendor);
  2. Add a new variable $image;
  3. Override __construct method, changing the parameter type to CustomPayloadNotificationBuilder. Set all the variables like is in PayloadNotification and also set the new variable $image.
  4. Override toArray method, setting all the properties like is in PayloadNotification and also set a new property image with $image value.

Something like this:

<?php

namespace App\Notifications\Messages;

use LaravelFCM\Message\PayloadNotification;
use App\Notifications\Messages\CustomPayloadNotificationBuilder;

class CustomPayloadNotification extends PayloadNotification // Extends vendor script
{
    protected $image; // New variable

    /**
     * CustomPayloadNotificationBuilder constructor.
     *
     * @param CustomPayloadNotificationBuilder $builder
     */
    public function __construct(CustomPayloadNotificationBuilder $builder) // Change the type of parameter
    {
        $this->title = $builder->getTitle();
        $this->body = $builder->getBody();
        $this->icon = $builder->getIcon();
        $this->sound = $builder->getSound();
        $this->badge = $builder->getBadge();
        $this->tag = $builder->getTag();
        $this->color = $builder->getColor();
        $this->clickAction = $builder->getClickAction();
        $this->bodyLocationKey = $builder->getBodyLocationKey();
        $this->bodyLocationArgs = $builder->getBodyLocationArgs();
        $this->titleLocationKey = $builder->getTitleLocationKey();
        $this->titleLocationArgs = $builder->getTitleLocationArgs();
        $this->image = $builder->getImage(); // Set image
    }

    /**
     * convert CustomPayloadNotification to array
     *
     * @return array
     */
    function toArray()
    {
        $notification = [
            'title' => $this->title,
            'body' => $this->body,
            'icon' => $this->icon,
            'sound' => $this->sound,
            'badge' => $this->badge,
            'tag' => $this->tag,
            'color' => $this->color,
            'click_action' => $this->clickAction,
            'body_loc_key' => $this->bodyLocationKey,
            'body_loc_args' => $this->bodyLocationArgs,
            'title_loc_key' => $this->titleLocationKey,
            'title_loc_args' => $this->titleLocationArgs,
            'image' => $this->image, // Set property image with $image value
        ];

        // remove null values
        $notification = array_filter($notification, function($value) {
            return $value !== null;
        });

        return $notification;
    }
}
  • Add a new script called CustomPayloadNotificationBuilder.php

Here you need to:

  1. Extends PayloadNotificationBuild (vendor);
  2. Add a new variable protected $image;
  3. Create the set/get methods to $image;
  4. Override build method, returning a new CustomPayloadNotification instead PayloadNotification.

Something like this:

<?php

namespace App\Notifications\Messages;

use LaravelFCM\Message\PayloadNotificationBuilder;
use App\Notifications\Messages\CustomPayloadNotification;

class CustomPayloadNotificationBuilder extends PayloadNotificationBuilder // Extends vendor script
{
    protected $image; // New variable

    /**
     * Set image
     *
     * @param string $image
     *
     * @return CustomPayloadNotificationBuilder
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }
    /**
     * Get image.
     *
     * @return null|string
     */
    public function getImage()
    {
        return $this->image;
    }
    /**
     * Build an CustomPayloadNotification
     * 
     * @return CustomPayloadNotification
     */
    public function build()
    {
        return new CustomPayloadNotification($this); // Change the object returned
    }
}
  • Reference CustomPayloadNotificationBuilder instead PayloadNotificationBuilder scripts in your code.
  • Use the method setImage

Your code should be something like this:

    use App\Notifications\Messages\CustomPayloadNotificationBuilder; // Add the reference on the top of your code

    // No changes before here [...]

    $notificationBuilder = new CustomPayloadNotificationBuilder($title); // Replace here
    $notificationBuilder->setClickAction('NOTIFICATION');
    $notificationBuilder->setBody($message)->setSound('default');
    $notificationBuilder->setTag(strtotime("now"));
    $notificationBuilder->setImage("Image URL here"); // Add an image

    // No changes after here [...]

you need to do some change in vendor for this

Step-1 : Go to the following url I am sharing here-

Laravel-FCM-master\Laravel-FCM-master\src\Message\PayloadNotification.php

Step-2 : here you have to add a instance variable as

protected $image;

Step - 3 find the public function __construct(PayloadNotificationBuilder $builder)

step -4 add $this->image = $builder->getImage(); in this function.

step -5 find the public function toArray()

step -6 add here 'image' => $this->image,

step -7 save and exit.

step -8 then follow this url in vendor again Laravel-FCM-master\Laravel-FCM-master\src\Message\PayloadNotificationBuilder.php:

step -9 add in above page

 /**
 * Indicates the image that can be displayed in the notification
 * Supports an url or internal image.
 *
 * @param string $image
 *
 * @return PayloadNotificationBuilder current instance of the builder
 */
public function setImage($image)
{
    $this->image = $image;

    return $this;
}

step - 10 then add

/**
 * Get image.
 *
 * @return null|string
 */
public function getImage()
{
    return $this->image;
}

step - 11 that's it, now you can easily able to add a new field in your controller where your code was written asked in question.

simply modify as

    $notificationBuilder = new PayloadNotificationBuilder($title);
    $notificationBuilder->setClickAction('NOTIFICATION');
 $notificationBuilder->setBody($message)->setImage("https://yourdoamin.com/yourdesiredimage.jpeg")->setSound('default');
    $notificationBuilder->setTag(strtotime("now"));

and send you will get exact what you are looking for.

Related