php - 如何实现像Facebook这样的通知系统?

标签 php mysql facebook

我想做一个类似Facebook的通知系统,但是我就是想不出办法,怎么实现。

我在这里看到了各种意见,比如有一张 table ,其他人说有两张。

不过,我正在尝试仅使用必要数据条目的最有效方法。 到目前为止,这是我的 notfication 表: id、uid、类型、读取( bool 值)、日期。

所以我想到了一个例子:

插入评论,调用通知函数插入我的uid(user_id),类型(评论,点赞等),read='1',NOW()。

我将读取设置为 1,因为我不想在发布内容时收到通知。然后我想通过将所有其他用户的读取设置为 0 来更新 notification,因为他们还没有阅读这篇新帖子。

但现在我正在考虑将在 5 秒后发布的另一篇文章。然后所有其他 read 条目再次设置为 0,这是正确的,但在这种情况下,我想不出为每个用户显示 (SELECT) 正确的结果。 因为没有指定哪个用户错过了哪个新通知。

希望大家关注我,有什么建议。如果我通常以错误的方式进行操作,我也会很感激您的建议。

最佳答案

首先,您必须知道您的通知是否适用于您的所有用途,还是可以针对特定用户。如果是 Facebook,我会说这是第二种选择。

我的第一个建议是找到一个实现此功能的开源项目(甚至可能是 2 个)并查看他们的代码。

我的第二个建议是写下此功能的所有要求,因为通常情况下,一个小的限制可能会导致对表结构的修改甚至添加一个新表(例如,是否可以将通知发送到一次有多个用户?一个用户可以向另一个用户发送通知吗?...)。

这是我要采用的方法,使用 2 个表,一个用于消息,一个用于与用户的 NtoN 关系: 表格通知

ID // auto increment ID
sender_id // Can be a subsystem or another user. To be defined / optional. Does not have to be a foreign key.
title // Title of the notification
body // The text message
type // warning, message, error or any notification class you can think of
... // you can add other options, like priority, attachment...

表通知_用户

notification_id // Foreign Key for the notification
receiver_id // Foreign Key for the user
is_read // is the notification read
is_deleted // is the notification deleted, this implements a Trash bin like feature
created_at // time at which the notification was sent
read_at // time at which the notification was read
deleted_at // Time at which the notification was deleted

关于php - 如何实现像Facebook这样的通知系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12526329/

相关文章:

facebook - 如何将facebook视频添加到og :video meta tag?

php - 在 mysql 查询中使用 PHP 表单

php - 在不知道当前位置的情况下查找上一个/下一个 ID(PHP 和 MySQL)

php - 为什么 COALESCE 订单结果会这样?

c# - 如何使用 wcf 服务获取 facebook 授权码

android - 无法从 Android SDK 4 中的 Facebook 获取个人资料信息

php - 如何在 MacOS 上安装 MySQLi

php - 使用 PHPExcel 读取包含命名范围的公式结果

php - 如何从 MySQL 数组中检索选中的复选框?

mysql - 如何获得给定日期的上周五和下周四?