2017-05-09 58 views
1

我似乎無法使實時通知工作。我已經在我的項目中實施了這些提要。但是當我的帖子中有人關注,喜歡或評論時,我需要實施實時通知。 我還實施了通知頁面(用戶可以查看以前的通知)。getstream laravel with streamjs實時通知

<script type="text/javascript"> 
var feed = client.feed('notification', '1', '1999'); 
function updateNotificationCallback(data) { 
    alert(data); 
} 
feed.subscribe(function callback(data) { 
    updateNotificationCallback(data); 
}); 
</script> 

我正在使用上面的代碼,它是從streamjs回購文檔中獲得的。我假設上面的代碼是檢查當前用戶是否有新通知的代碼?沒有錯誤,但我假設有人跟着我,updateNotificationCallback將被調用。

我錯過了什麼?

目前,我使用下面的代碼來通知當前用戶他是否有一些未讀通知。 notification toolbar

當用戶點擊通知圖標時,它會將他重定向到通知頁面,該頁面還會將所有檢索到的通知設置爲「讀取」,從而重置計數器。

HomeController.php

public function myNewNotifications() { 

    $new = 0; 

    try { 
    $notification_feed = FeedManager::getNotificationFeed(Auth::user()->id)->getActivities()['unread']; 
    $new = (int)$notification_feed; 
    } catch(ErrorException $e) { 
    $new = 0; 
    } 

    return response()->json([ 
    'new' => (int)$new 
    ]); 
} 

VueJs部件來檢索未讀通知計數:當活動被添加或去除,以進料

<template> 
<span class="badge notificationCounter" v-if="notificationsCount >= 1">{{ notificationsCount }}</span> 
</template> 
<script> 
    export default { 
    mounted() { 
     this.notificationsCount = this.initialCount; 
     this.loadData(); 
     var that = this; 
     setInterval(function() { 
     that.loadData(); 
     }, 30000); 
    }, 
    data() { 
     return { 
     notificationsCount: 0 
     } 
    }, 
    props: [ 
     'initialCount' 
    ], 
    methods: { 
     loadData: function() { 
     axios.get('/my/activities/notifications/new').then((response) => { 
      this.notificationsCount = response.data.new; 
     }); 
     } 
    } 
    } 
</script> 
+0

您可以將回調添加到由feed.subscribe返回的諾言嗎?例如:feed.subscribe()。then(successCallback,failCallback); –

+0

嗨@TommasoBarbugli,我也試過這個,但它似乎只是在您喜歡或追隨某些其他帖子或用戶時觸發回調。我希望有這樣的代碼可以告訴你什麼時候有一些人在說,夏威夷人會跟隨你或喜歡你的帖子。 –

回答

0

實時更新只燒成。

如果您想發送關於關注/取消關注的通知,最簡單的方法是爲其創建活動(例如,Tommaso遵循Rayzor),並將其發送給關注用戶的通知源。

如果您使用Laravel集成,您可以通過將活動特徵添加到Follow模型來實現此目的。

+0

您好Tommaso,感謝您的投入。這是我現在正在追求的方法,所以我會保留我的解決方案。 –