gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:14:31 28 4
gpt4 key购买 nike

如何使用 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();

最佳答案

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

  • 在应用程序中创建一个新路径:app/Notifications/Message
  • 添加一个名为 CustomPayloadNotification.php 的新脚本

  • 在这里你需要:
  • 扩展 有效载荷通知 (小贩);
  • 添加新变量 $图片 ;
  • 覆盖 __构造方法,将参数类型更改为 CustomPayloadNotificationBuilder .在 PayloadNotification 中设置所有变量,并设置新变量 $图片 .
  • 覆盖 数组 方法,设置所有属性,如在 PayloadNotification 中,并设置一个新的 楼盘图片 $图片值(value)。

  • 像这样的东西:
    <?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 的新脚本

  • 在这里你需要:
  • 扩展 PayloadNotificationBuild (小贩);
  • 添加一个新变量 protected $图片 ;
  • 创建 设置/获取 $image 的方法;
  • 覆盖 构建 方法,返回一个新的 CustomPayloadNotification 而是有效载荷通知。

  • 像这样的东西:
    <?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 [...]

    关于php - 如何使用 brozot/Laravel-FCM 在推送通知上添加图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61009262/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com