Laravel Echo 未收听通知
Laravel Echo not listening notification
我正在尝试通过 laravel echo 获取通知,但它不起作用。我可以在推送器调试控制台上看到所有通知,但在浏览器控制台上看不到。浏览器控制台中未显示任何错误。
app.js
Echo.private(`App.User.3`)
.notification((notification) => {
console.log(notification.type);
});
BidOnAuction.php
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\BroadcastMessage;
class BidOnAuction extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
protected $user;
protected $auction;
public function __construct($user,$auction)
{
$this->user=$user;
$this->auction=$auction;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database','broadcast'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
// public function toMail($notifiable)
// {
// return (new MailMessage)
// ->line('The introduction to the notification.')
// ->action('Notification Action', url('/'))
// ->line('Thank you for using our application!');
// }
public function toDatabase($notifiable)
{
return [
'user_name' => $this->user->name,
'auction_title' => $this->auction->price,
];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'user_name' => $this->user->name,
'auction_title' => $this->auction->price,
]);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
我试过这个答案,但它不起作用
我想在 laravel 前端收到通知
我尝试访问的频道是私人频道,因此只有授权用户才能访问频道。为此,我在广播路由中传递了中间件
Providers/BroadcastServiceProvider.php
public function boot()
{
Broadcast::routes(['middleware' => ['web']]);
require base_path('routes/channels.php');
}
我正在尝试通过 laravel echo 获取通知,但它不起作用。我可以在推送器调试控制台上看到所有通知,但在浏览器控制台上看不到。浏览器控制台中未显示任何错误。
app.js
Echo.private(`App.User.3`)
.notification((notification) => {
console.log(notification.type);
});
BidOnAuction.php
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\BroadcastMessage;
class BidOnAuction extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
protected $user;
protected $auction;
public function __construct($user,$auction)
{
$this->user=$user;
$this->auction=$auction;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database','broadcast'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
// public function toMail($notifiable)
// {
// return (new MailMessage)
// ->line('The introduction to the notification.')
// ->action('Notification Action', url('/'))
// ->line('Thank you for using our application!');
// }
public function toDatabase($notifiable)
{
return [
'user_name' => $this->user->name,
'auction_title' => $this->auction->price,
];
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'user_name' => $this->user->name,
'auction_title' => $this->auction->price,
]);
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
我试过这个答案,但它不起作用
我想在 laravel 前端收到通知
我尝试访问的频道是私人频道,因此只有授权用户才能访问频道。为此,我在广播路由中传递了中间件
Providers/BroadcastServiceProvider.php
public function boot()
{
Broadcast::routes(['middleware' => ['web']]);
require base_path('routes/channels.php');
}