【发布时间】:2021-10-23 02:14:24
【问题描述】:
我刚刚在私人频道中使用推送器设置了我的 Laravel 通知。开发服务器(127.0.0.1:8000)上一切正常。我经常收到通知,但是当我尝试从 localhost(http://localhost/) 运行时出现身份验证错误“http://localhost/broadcasting/auth 404 not found”。这是我的代码:
Reportnotification.php
public function toArray($notifiable)
{
return [
'message'=>$this->details,
'link'=> $this->link,
];
}
public function toBroadcast($notifiable): BroadcastMessage
{
return new BroadcastMessage([
'message' => "$this->details",
'link'=> "$this->link"
]);
}
事件/报告通知
class ReportNotification implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
// public $username;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message)
{
$message = $this->message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('notification');
}
监听器/SendReportNotification
public function handle($event)
{
$admins = User::where('role',1)->get();
ReportNotification::send($admins, new ReportNotification($event->user));
}
Channels.php
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
master.blade.php
<script>
var id = {{Auth::user()->id}}
Echo.private('App.Models.User.'+id)
.notification((ReportNotification) => {
console.log(ReportNotification.message);
console.log(ReportNotification.link);
});
</script>
这些是我的代码。在 "127.0.0.1:8000" 中一切正常,但在 "localhost"
中无法正常工作【问题讨论】:
标签: php laravel push-notification pusher