如何使用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 的新脚本
这里你需要:
- 扩展PayloadNotification(供应商(
- 添加一个新变量$image
- 重写__construct方法,将参数类型更改为CustomPayloadNotificationBuilder。设置PayloadNotification中的所有变量,并设置新变量$image
- 覆盖toArray方法,设置PayloadNotification中的所有属性,并使用$image值设置新的属性映像
类似这样的东西:
<?php
namespace AppNotificationsMessages;
use LaravelFCMMessagePayloadNotification;
use AppNotificationsMessagesCustomPayloadNotificationBuilder;
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(供应商(
- 添加一个受保护的新变量$image
- 创建set/get方法到$image
- 重写build方法,返回一个新的CustomPayloadNotification而不是PayloadNotification
类似这样的东西:
<?php
namespace AppNotificationsMessages;
use LaravelFCMMessagePayloadNotificationBuilder;
use AppNotificationsMessagesCustomPayloadNotification;
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 AppNotificationsMessagesCustomPayloadNotificationBuilder; // 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 [...]
您需要对的供应商进行一些更改
步骤1:转到我在这里分享的以下网址-
Laravel FCM master\Laravel FCM master\src\Message\PayloadNotification.php
步骤2:在这里你必须添加一个实例变量作为
受保护$image;
步骤-3找到公共函数__construct(PayloadNotificationBuilder$builder(
步骤-4添加$this->image=$builder->getImage((;在这个函数中。
步骤-5找到公共函数toArray((
步骤-6在此处添加"image"=>这->图像,
步骤-7保存并退出。
步骤-8然后在供应商中再次遵循此url Laravel FCM master\Laravel FCC-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"));
发送给你会得到你想要的确切信息。