【问题标题】:How to add image on push notification using brozot / Laravel-FCM如何使用 brozot / Laravel-FCM 在推送通知上添加图像
【发布时间】:2021-03-16 20:53:42
【问题描述】:

如何使用 brozot / Laravel-FCM 在推送通知上添加图片?

我正在正确发送通知,但我想知道如何发送带有通知的图像?

我试过这段代码,但没有用

        $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();

【问题讨论】:

    标签: php laravel firebase-cloud-messaging


    【解决方案1】:

    您需要创建一个继承供应商脚本的自定义脚本并在其上添加一些属性。

    • 在应用中新建路径:app/Notifications/Message

    • 添加一个名为 CustomPayloadNotification.php 的新脚本

    这里你需要:

    1. 扩展 PayloadNotification(供应商);
    2. 添加一个新变量$image;
    3. 重写 __construct 方法,将参数类型更改为 CustomPayloadNotificationBuilder。在 PayloadNotification 中设置所有变量,并设置新变量 $image
    4. 重写toArray方法,设置PayloadNotification中的所有属性,并设置一个新的属性图像,其值为$image。李>

    类似这样的:

    <?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;
        }
    }
    
    • 添加一个名为 CustomPayloadNotificationBuilder.php 的新脚本

    这里你需要:

    1. 扩展 PayloadNotificationBuild(供应商);
    2. 添加新变量 protected $image;
    3. 为 $image 创建 set/get 方法;
    4. 覆盖 build 方法,返回一个新的 CustomPayloadNotification 而不是 PayloadNotification。

    类似这样的:

    <?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
        }
    }
    
    • 在您的代码中参考 CustomPayloadNotificationBuilder 而不是 PayloadNotificationBuilder 脚本。
    • 使用setImage方法

    你的代码应该是这样的:

        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 [...]
    

    【讨论】:

      【解决方案2】:

      您需要为此更改供应商

      第 1 步:转到我在此处分享的以下网址-

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

      第二步:这里你必须添加一个实例变量

      受保护的 $image;

      Step - 3 找到公共函数 __construct(PayloadNotificationBuilder $builder)

      步骤-4添加 $this->image = $builder->getImage();在这个函数中。

      步骤-5找到公共函数toArray()

      步骤 -6 在此处添加 'image' => $this->image,

      步骤-7保存并退出。

      步骤 -8 然后再次在供应商中按照此 url Laravel-FCM-master\Laravel-FCM-master\src\Message\PayloadNotificationBuilder.php:

      步骤-9在上面的页面中添加

       /**
       * 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;
      }
      

      步骤 - 10 然后添加

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

      步骤 - 11 就是这样,现在您可以轻松地在控制器中添加一个新字段,其中您的代码被询问。

      只需修改为

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

      然后发送给您,您将得到您正在寻找的确切信息。

      【讨论】:

      • 您是否建议读者编辑其(Composer)供应商文件夹中的文件?如果是这样,这是一个糟糕的建议——这个文件夹不应该被提交给版本控制,它应该能够被销毁和重新安装,而不影响项目的正确运行。如果需要修改一个类,那么项目级类应该从它extend
      • 从不需要编辑 vendor 文件夹中的类,除非是在遗留项目环境中最可怕的情况,即便如此,这也是一个骇人听闻的黑客行为。几乎不能夸大它是多么糟糕的做法。您是否主张将vendor 文件夹提交给版本控制?
      • 我想知道你是否不知道这里的“标记”是什么意思。我没有标记答案,我已发表评论。 “举报”是指使用举报功能来吸引版主或队列审阅者的注意。
      • 在软件工程中,能够接受批评是必不可少的。在这里,如果我看到有人传播了不好的建议,我会发表评论,我希望提供建议的一方愿意为他们提供的建议进行愉快的辩护,或者根据我的反馈修改建议。这里没有关于谁是“最聪明”的竞争——成年人不会做出这种行为。
      • 我假设 PayloadNotificationBuilder 可以在 vendor/ 目录之外的类中扩展 - 你认为这行不通吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-06
      • 1970-01-01
      • 2017-12-23
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多