nas*_*a c 6 php laravel firebase-cloud-messaging
如何使用 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();
Run Code Online (Sandbox Code Playgroud)
小智 5
您需要创建一个继承供应商脚本的自定义脚本并在其上添加一些属性。
在应用程序中创建一个新路径:app/Notifications/Message
添加一个名为 CustomPayloadNotification.php 的新脚本
在这里你需要:
像这样的东西:
<?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;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里你需要:
像这样的东西:
<?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
}
}
Run Code Online (Sandbox Code Playgroud)
你的代码应该是这样的:
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 [...]
Run Code Online (Sandbox Code Playgroud)
小智 1
为此,您需要对供应商进行一些更改
第 1 步:转到我在这里分享的以下网址 -
Laravel-FCM-master\Laravel-FCM-master\src\Message\PayloadNotification.php
步骤2:在这里你必须添加一个实例变量
受保护的$图像;
步骤 - 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;
}
Run Code Online (Sandbox Code Playgroud)
步骤 - 10 然后添加
/**
* Get image.
*
* @return null|string
*/
public function getImage()
{
return $this->image;
}
Run Code Online (Sandbox Code Playgroud)
步骤 - 11 就是这样,现在您可以轻松地在控制器中添加一个新字段,其中您的代码是有问题的。
只需修改为
$notificationBuilder = new PayloadNotificationBuilder($title);
$notificationBuilder->setClickAction('NOTIFICATION');
$notificationBuilder->setBody($message)->setImage("https://yourdoamin.com/yourdesiredimage.jpeg")->setSound('default');
$notificationBuilder->setTag(strtotime("now"));
Run Code Online (Sandbox Code Playgroud)
并发送给您将得到您正在寻找的确切内容。
| 归档时间: |
|
| 查看次数: |
3251 次 |
| 最近记录: |