laravel - 在 Laravel 中仅向 1 个用户发送推送通知
问题描述
所以我将 Laravel Event 与 Pusher 一起使用,到目前为止,如果我在没有任何额外条件的情况下使用公共频道,我可以在我的刀片中获取我的数据作为通知。
当我尝试仅向 1 个用户而不是所有人发送通知时,问题就开始了,我已经尝试过PrivateChannel
,但我得到了:
[Vue warn]: Error in created hook: "ReferenceError: bidId is not defined"
and
ReferenceError: bidId is not defined
逻辑
仅向项目所有者发送通知(他/她ID
保存在项目表中)
Vue 组件
<template>
<li class="nav-item dropdown">
<a id="navbarDropdown" class="nav-link icon-menu dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
<span class="sr-only">Notifications</span>
<i class="far fa-bell"></i>
<span class="notification-dot"></span>
</a>
<div class="dropdown-menu notifications dropdown-menu-right" aria-labelledby="navbarDropdown">
<a class="dropdown-header"><strong>You have {{bids.length}} new notifications</strong></a>
<a class="dropdown-item" v-for="bid in bids">
<div class="media">
<div class="media-left">
<i class="fa fa-fw fa-flag-checkered text-muted"></i>
</div>
<div class="media-body">
<p class="text">{{bid.message}}</p>
<span class="timestamp">{{bid.created_at}}</span>
</div>
</div>
</a>
<a class="dropdown-item more">See all notifications</a>
</div>
</li>
</template>
<script>
export default {
data() {
return{
bids:[],
}
},
created(){
this.fetchBids();
},
methods:{
fetchBids(){
// Echo.channel('bidplaced') //tested with public channel
Echo.private(`bidplaced.${bidId}`)
.listen('BidPlaced', (e) => {
this.bids.push(e.bid);
});
},
},
}
</script>
刀
<notifications></notifications>
控制器
$bid->save();
event(new BidPlaced($bid)); //firing the event
// this is 1 way to get the user i need to receive notification
$user = $bid->project->user;
事件
class BidPlaced implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $bid;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Bid $bid)
{
$this->bid = $bid;
}
public function broadcastOn()
{
// return ['bidplaced']; //this is working as public channel
return new PrivateChannel('bidplaced.'.$this->bid->project->user_id);
}
问题
我怎样才能让我的项目所有者成为这些通知的接收者?
解决方案
可能有点晚了,但希望这对某人有帮助!
控制器
broadcast(new BidPlaced($user))->toOthers();
事件
use App\User
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function broadcastOn()
{
return new PrivateChannel('notification.'.$this->user->id);
}
Vue 组件
created() {
Echo.private('notification.'+auth.user.id)
.listen('BidPlaced', (e) => {
console.log(e);
});
},
推荐阅读
- python - TypeError:“PosixPath”类型的参数不可迭代,旧解决方案不起作用
- c# - 为什么我不能全部删除?
- r - 将数字转换为字符串,同时在 R 中保持字符不变
- devops - 持续部署是否意味着我们不使用预生产环境?
- python - 如何在 Json 响应 Python 中合并 order_id
- amazon-s3 - 如何使用 Cloudfront 在没有公共访问权限的私有 S3 存储桶上托管静态网站?
- react-native - React Native:_this.setState 不是 react-navigation 应用程序中抛出的函数
- python - openCV中是否有一种方法可以裁剪图像并将未裁剪的区域保留为黑色?
- framemaker - “过滤器遇到错误,无法完成翻译”是什么意思?
- reactjs - 视频无法在 React 中的移动设备上播放