我遇到問題。 我使用推送器進行實時通知,當它運行時,它會通知我使用noty, ,但我也希望它在類似於facebook的視圖中實時顯示新通知的編號。實時通過laravel5.2,vue.js和推送器獲取數據
<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));
}
}
});
計劃作業控制器。此方法每分鐘運行一次。
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方法是一種單一方法,您需要不斷地收聽推送器事件。在文檔中他們說你用susbcirbe方法做到這一點?你的Vue代碼在哪裏? –
謝謝!我現在正在研究訂閱推送服務。 –