【问题标题】:Send Notifications from Multiple Private Channels in Laravel Broadcasts在 Laravel 广播中从多个私有频道发送通知
【发布时间】:2020-08-25 23:14:41
【问题描述】:

我配置了pusherLaravel BroadcastingLaravel Echo 来向我的用户发送通知。它适用于单个私人频道。

我有两个私人频道。

  1. App.User{id}
  2. 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;
    }
}

我的 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>

谁能回答这个问题?

【问题讨论】:

  • 你不能从一个函数做多个返回。为什么你想要不同的频道?如果您想要相同的事件,为什么不只收听用户频道?
  • 我确实有多个活动。所以我需要发送不同的事件。
  • 那么您将不得不手动广播它们。检查:laravel.com/docs/7.x/broadcasting#using-example-application
  • 这里我正在尝试发送带有数据库通知的广播通知。我使用相同的 Notification 类来使用 toBroadcasttoDatabase 。我试图通过`laravel 通知类`而不是laravel event class 发送通知。

标签: php laravel broadcast pusher laravel-echo


【解决方案1】:

而不是:

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
    ];
}

【讨论】:

  • 我试过了。它的错误称为:"message": "Array to string conversion",
【解决方案2】:

试试这个:

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

        return $channels;

【讨论】:

  • 我收到一个名为Array to string conversion的错误。
【解决方案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;

【讨论】:

    猜你喜欢
    • 2017-09-23
    • 1970-01-01
    • 2021-06-01
    • 2018-02-23
    • 2019-05-16
    • 2018-03-03
    • 2021-11-15
    • 2021-07-15
    相关资源
    最近更新 更多