在PHP中构建多通知系统的最佳方法

Dwa*_*ton 17 php mysql database-design codeigniter database-schema

我目前正在开发一款移动应用程序,可让您向朋友询问收藏,它是HTML5前端和PHP后端.我不知道建立通知系统的最佳方式是什么,更多的是数据库模式而不是实际的代码本身.

移动应用程序流程如下:

  • 用户请求帮助
  • 用户可以选择; 通知所有朋友,通知最喜欢的朋友或通知朋友
  • 根据用户的选择,人们会收到通知

在PHP和MySQL中执行此操作的最佳方法是什么?我并不是真的要求任何人给我写PHP代码,而是映射出最理想的MySQL表和字段模式,因为这是我目前所坚持的.在此先感谢您的帮助.

Sho*_*hoe 29

您可以创建通知表,例如:

from_user | to_user | notification | seen
Run Code Online (Sandbox Code Playgroud)

然后,只要您想要通知用户,您只需添加包含所需信息的记录并设置seen0/ false.

然后,当用户读取通知时,将该参数设置为1/ true.

例:

from_user | to_user | notification | seen
    -          -          -           -
Run Code Online (Sandbox Code Playgroud)

用户john通知用户jeff:

from_user | to_user | notification | seen
   john      jeff       whatever..    0
Run Code Online (Sandbox Code Playgroud)

用户杰夫阅读通知:

from_user | to_user | notification | seen
   john      jeff       whatever..    1  
Run Code Online (Sandbox Code Playgroud)

  • 简单但可靠的方法+1 (2认同)

Lau*_*nce 5

为什么不只在称为“通知”的表中列出所有通知

id | user_id | from_user_id | notification
Run Code Online (Sandbox Code Playgroud)
  • id =通知ID
  • user_id =通知的对象是谁?
  • from_user_id =谁发送了通知?
  • 通知=消息

然后作为伪代码:

// Create a notification from User A to User B
$this->db->insert ('notifications', array ('user_id' => $friends_id, 'from_user_id' => $current_user_id, 'notification' => $message));

 // The meanwhile, on your home page or somewhere, wherever you want to display notifications
 $this->db->where ('user_id', $current_user_id)
 $notifications = $this->db->get ('user_id');
 foreach ($notifications as $notification)
 {
         // Display the notification as needed
         // Optionally delete the notification as it is displayed if it is a "one off" alert only
 }
Run Code Online (Sandbox Code Playgroud)


Har*_*ker 5

扩展杰弗里的答案:

id  |  from_user | to_user | notification | seen | timestamp
 -         -          -          -           -         -
Run Code Online (Sandbox Code Playgroud)

时间戳将帮助您识别特定通知发生的时刻。您还可以通过使用timeago.jsLivestamp.js扩展脚本来创建时刻

它还将帮助您创建通知时间表,并且通知的维护会更简单。