2017-05-09 62 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,感谢您的投入。这是我现在正在追求的方法,所以我会保留我的解决方案。 –