Php 在Laravel广播中从多个专用频道发送通知

Php 在Laravel广播中从多个专用频道发送通知,php,laravel,broadcast,pusher,laravel-echo,Php,Laravel,Broadcast,Pusher,Laravel Echo,我配置了pusher,Laravel-Broadcasting,Laravel-Echo向我的用户发送通知。对于单个专用通道,它工作正常 我有两个私人频道 App.User{id} YouthAdd.YouthAdd{id} 但是通知只通过App.User{id}通道传递 我如何也从其他渠道发送通知 我的用户类 namespace App; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Foundation\Auth\Us

我配置了
pusher
Laravel-Broadcasting
Laravel-Echo
向我的用户发送通知。对于单个专用通道,它工作正常

我有两个私人频道

  • App.User{id}
  • YouthAdd.YouthAdd{id}
  • 但是通知只通过
    App.User{id}
    通道传递

    我如何也从其他渠道发送通知

    我的用户类
    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;
        }
    }
    
    My 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>
    
    
    var userId=$('meta[name=“userId”]”)。attr('content');
    Echo.private('App.User.'+userId)
    .通知((通知)=>{
    toastr.warning(notification.title,notification.name,{closeButton:true,timeOut:5000000});
    });
    Echo.private('YouthAdd.YouthAdd.'+userId)
    .通知((通知)=>{
    console.log(notification.youth);
    toastr.error(notification.youth.name,notification.youth.nic,{closeButton:true,timeOut:5000000});
    });
    
    谁能回答这个问题

    而不是:

    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
        ];
    }
    
    试试这个:

            $channelNames = ['App.User.' . $this->id, 'YouthAdd.YouthAdd.'.$this->id];
            $channels = [];
            foreach ($channelNames as $channel) {
                $channels[] = new PrivateChannel($channel);
            }
    
            return $channels;
    

    您不能从单个函数执行多次返回。为什么你想要不同的频道?如果您想要相同的事件,为什么不直接在用户频道上收听呢?我确实有多个事件。所以我需要发送不同的事件。然后你必须手动播放它们。检查:我正在尝试发送广播通知和数据库通知。我使用了相同的通知类,通过
    toBroadcast
    toDatabase
    实现了这一点。我试图通过“laravel通知类”而不是
    laravel事件类
    发送通知。我尝试了。它的获取错误名为:
    “message”:“数组到字符串转换”
    我获取的错误名为
    数组到字符串转换