getstream laravel 带有 streamjs 实时通知

getstream laravel with streamjs live notification

我似乎无法使实时通知正常工作。我已经在我的项目中实施了提要。但我需要在有人关注、点赞或评论我的 post 时实施实时通知。 我还实现了通知页面(用户可以在其中查看以前的通知)。

<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

当用户点击通知图标时,它会将他重定向到通知页面,该页面还会将所有检索到的通知设置为 "read",从而重置计数器。

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>

实时更新仅在 activity 添加或删除到 Feed 时触发。

如果您想为 follow/unfollows 发送通知,最简单的方法是为其创建一个 activity(例如,Tommaso 关注 Rayzor)并将其发送到关注用户的通知提要。

如果您使用 Laravel 集成,您可以通过将 Activity 特征添加到您的关注模型来实现。