我似乎无法使实时通知工作。我已经在我的项目中实施了这些提要。但是当我的帖子中有人关注,喜欢或评论时,我需要实施实时通知。 我还实施了通知页面(用户可以查看以前的通知)。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>
您可以将回调添加到由feed.subscribe返回的诺言吗?例如:feed.subscribe()。then(successCallback,failCallback); –
嗨@TommasoBarbugli,我也试过这个,但它似乎只是在您喜欢或追随某些其他帖子或用户时触发回调。我希望有这样的代码可以告诉你什么时候有一些人在说,夏威夷人会跟随你或喜欢你的帖子。 –