为了账号安全,请及时绑定邮箱和手机立即绑定

在 Laravel 广播中从多个私有频道发送通知

在 Laravel 广播中从多个私有频道发送通知

PHP
温温酱 2023-03-04 17:11:05
我配置pusher, Laravel Broadcasting,Laravel Echo向我的用户发送通知。它适用于单个私人频道。我有两个私人频道。App.User{id}YouthAdd.YouthAdd{id}App.User{id}但是通知仅通过Channel传递我怎样才能从其他渠道发送通知呢?我的用户类namespace App;use Illuminate\Broadcasting\PrivateChannel;use Illuminate\Foundation\Auth\User as Authenticatable;use Illuminate\Notifications\Notifiable;class User extends Authenticatable{    use Notifiable;    /**     * The channels the user receives notification broadcasts on.     *     * @return string     */    public function receivesBroadcastNotificationsOn()    {        return 'App.User.'.$this->id;        return 'YouthAdd.YouthAdd.'.$this->id;    }}我的 Channels.php 路由文件Broadcast::channel('App.User.{id}', function ($user, $id) {    return (int) $user->id === (int) $id;});Broadcast::channel('YouthAdd.YouthAdd.{id}', function ($user, $id) {    return (int) $user->id === (int) $id;});我的前端<script>  var userId = $('meta[name="userId"]').attr('content');    Echo.private('App.User.' + userId)    .notification((notification) => {        toastr.warning(notification.title, notification.name, {closeButton: true, timeOut: 5000000});    });      Echo.private('YouthAdd.YouthAdd.' + userId)      .notification((notification) => {          console.log(notification.youth);          toastr.error(notification.youth.name, notification.youth.nic, {closeButton: true, timeOut: 5000000});      });    </script>任何人都可以回答这个问题吗?
查看完整描述

3 回答

?
胡说叔叔

TA贡献1804条经验 获得超8个赞

代替 :


public function receivesBroadcastNotificationsOn()

{

    return 'App.User.'.$this->id;

    return 'YouthAdd.YouthAdd.'.$this->id;

}

尝试这个:


public function receivesBroadcastNotificationsOn()

{

     return [

        'App.User.'.$this->id,

        'YouthAdd.YouthAdd.'.$this->id

    ];

}


查看完整回答
反对 回复 2023-03-04
?
狐的传说

TA贡献1804条经验 获得超3个赞

这对我有用。


$channels = array();


        foreach ($this->athletes as $athlete) {

            array_push($channels, new PrivateChannel('athlete.' . $athlete->user->id));

        }


        array_push($channels, new PrivateChannel('user.' . $this->user->id));


return $channels;


查看完整回答
反对 回复 2023-03-04
?
冉冉说

TA贡献1877条经验 获得超1个赞

尝试这个:


        $channelNames = ['App.User.' . $this->id, 'YouthAdd.YouthAdd.'.$this->id];

        $channels = [];

        foreach ($channelNames as $channel) {

            $channels[] = new PrivateChannel($channel);

        }


        return $channels;


查看完整回答
反对 回复 2023-03-04
  • 3 回答
  • 0 关注
  • 149 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号