首先,您必须知道您的通知是否广泛应用于您的所有用途,或者可以是特定于用户的。如果是 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