首页 > 解决方案 > 在 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);
    }

问题

我怎样才能让我的项目所有者成为这些通知的接收者?

标签: laravelvue.js

解决方案


可能有点晚了,但希望这对某人有帮助!

控制器

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);
            });
    },

推荐阅读