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

我配置pusherLaravel Broadcasting,Laravel Echo向我的用户发送通知。它适用于单个私人频道。

我有两个私人频道。

  1. App.User{id}

  2. 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>

任何人都可以回答这个问题吗?


温温酱
浏览 150回答 3
3回答

胡说叔叔

代替 :public function receivesBroadcastNotificationsOn(){&nbsp; &nbsp; return 'App.User.'.$this->id;&nbsp; &nbsp; return 'YouthAdd.YouthAdd.'.$this->id;}尝试这个:public function receivesBroadcastNotificationsOn(){&nbsp; &nbsp; &nbsp;return [&nbsp; &nbsp; &nbsp; &nbsp; 'App.User.'.$this->id,&nbsp; &nbsp; &nbsp; &nbsp; 'YouthAdd.YouthAdd.'.$this->id&nbsp; &nbsp; ];}

狐的传说

这对我有用。$channels = array();&nbsp; &nbsp; &nbsp; &nbsp; foreach ($this->athletes as $athlete) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($channels, new PrivateChannel('athlete.' . $athlete->user->id));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; array_push($channels, new PrivateChannel('user.' . $this->user->id));return $channels;

冉冉说

尝试这个:&nbsp; &nbsp; &nbsp; &nbsp; $channelNames = ['App.User.' . $this->id, 'YouthAdd.YouthAdd.'.$this->id];&nbsp; &nbsp; &nbsp; &nbsp; $channels = [];&nbsp; &nbsp; &nbsp; &nbsp; foreach ($channelNames as $channel) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $channels[] = new PrivateChannel($channel);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $channels;
打开App,查看更多内容
随时随地看视频慕课网APP