【发布时间】:2016-07-20 08:06:38
【问题描述】:
我有问题。 我正在使用推送器进行实时通知,当它运行时它会使用 noty 通知我, 但我也希望它在我的视图中实时显示新通知的数量,类似于 facebook。
<span class="label label-warning" v-show="new_count">@{{ new_count }}<!-- get the new notifications --></span>
我该怎么做?
这是我的代码。
nav.js
new Vue({
el: '#nav',
data: {
new_count: 0
},
methods: {
getNewCount: function () {
this.$http.get('cron', function(new_count){
console.log(new_count);
this.new_count = new_count;
}.bind(this));
}
}
});
Cron 作业控制器。此方法每分钟运行一次。
class CronJobController extends Controller
{
public function getIndex()
{
$old_fail_notification = FailNotification::where('seen', 0)->count();
$subjects_with_fail = Grade::where('grade', 'F')->groupBy('subject_id')->orderBy('subject_id', 'ASC')->get();
return response()->json(['new_count' => 2]); //send this to navbar
foreach ($subjects_with_fail as $subject) {
$subject_to_check = Grade::where('grade', 'F')->where('subject_id', $subject->subject_id)->where('reviewed', '0')->get(); //add if grade is IC or D
if (count($subject_to_check) >= 3) {
$failed = new FailNotification();
$failed->message = '[subject-'.$subject->subject_id.'] can now be opened.';
$failed->save();
foreach ($subject_to_check as $subject) {
$subject = Grade::find($subject->id);
$subject->reviewed = 1;
$subject->save();
}
}
}
$fail_notification = FailNotification::all()->count();
//UPDATE NOTIFICATION COUNT HERE REAL TIME USING AJAX
if ($fail_notification > $old_fail_notification) {
$new_notification_count = $fail_notification - $old_fail_notification;
Pusher::trigger('test-channel', 'test-event', [
'name' => 'You have a new',
'message' => 'Notification'
]);
Pusher::trigger('navbar-channel', 'navbar-event', [
'new_notif_count' => $new_notification_count,
]);
return response()->json(['new_count' => $new_notification_count]); //send this to navbar
}
}
}
请告诉我我做错了什么以及如何做对。
【问题讨论】:
-
你在哪里订阅推送服务?如下所述,vue.resource 中的 get 方法是单一方法,需要不断监听 pusher 事件。在文档中,他们说您使用 susbcirbe 方法来做到这一点?你的 Vue 代码在哪里?
-
谢谢!我现在正在研究订阅推送服务。
标签: laravel notifications real-time vue.js pusher