【发布时间】:2020-08-25 23:14:41
【问题描述】:
我配置了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;
}
}
我的 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 类来使用
toBroadcast和toDatabase。我试图通过`laravel 通知类`而不是laravel event class发送通知。
标签: php laravel broadcast pusher laravel-echo