问题描述
我正在使用 laravel8 中的公共推送器频道发送通知.. 我怎样才能将其转换为私人频道,以便仅向所选用户发送通知.. 管理员从下拉菜单中选择了哪个?
这是我的控制器
public function notification(MessageRequest $request){
$data =[
'title' => $request->title,'body' => $request->body,// 'users' => $request->users,];
event(new NewNotification($data));
return redirect()->back()->with(['success'=>'success']);
}
我的活动:
class NewNotification implements Shouldbroadcast
{
use dispatchable,InteractsWithSockets,SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $title;
public $body;
// public $users;
public function __construct($data=[])
{
$this->title = $data['title'];
$this->body = $data['body'];
// $this->users = $data['users'];
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new Channel('notification');
}
}
这是布局:
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://js.pusher.com/7.0/pusher.min.js"></script>
<script>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('Meta[name="csrf-token"]').attr('content')
}
});
// Enable pusher logging - don't include this in production
Pusher.logToConsole = true;
var pusher = new Pusher('e7a414640973c420f866',{
// cluster: 'mt1'
});
</script>
<script src="{{asset('js/pusherNotifications.js')}}"></script>
和javascript文件是:
var notificationsWrapper = $('.dropdown-notifications');
var notificationsToggle = notificationsWrapper.find('a[data-toggle]');
var notificationsCountElem = notificationsToggle.find('span[data-count]');
var notificationsCount = parseInt(notificationsCountElem.data('count'));
var notifications = notificationsWrapper.find('li.scrollable-container');
// Subscribe to the channel we specified in our Laravel Event
var channel = pusher.subscribe('notification');
// Bind a function to a Event (the full Laravel class)
channel.bind('App\\Events\\NewNotification',function (data) {
var existingNotifications = notifications.html();
var newNotificationHtml = `<a href="`+data.title+`">
<div class="media-body"><h6 class="media-heading text-right">` + data.title + `</h6>
<p class="notification-text font-small-3 text-muted text-right">` + data.body + `</p><small style="direction: ltr;">
</small></div></div></a>`;
notifications.html(newNotificationHtml + existingNotifications);
notificationsCount += 1;
notificationsCountElem.attr('data-count',notificationsCount);
notificationsWrapper.find('.notif-count').text(notificationsCount);
notificationsWrapper.show();
});
任何帮助,
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)