
在Laravel应用中,当短时间内有大量事件触发通知时,用户可能会收到过多的重复或相似通知,导致体验不佳。本文将详细介绍如何通过优化Laravel的数据库通知机制,实现在特定时间窗口内合并或更新现有通知,而非创建新的通知,从而有效减少通知数量,提升用户体验。
在现代Web应用中,通知是与用户互动的重要手段。然而,如果通知系统设计不当,例如在短时间内针对同一事件或同一类事件发送大量独立通知,用户体验将大打折扣,可能导致“通知疲劳”,甚至让用户选择关闭通知功能。为了解决这一问题,我们需要一种机制,能够在特定条件下对通知进行“去重”或“合并”,例如,在30分钟内,将关于同一主题的多个通知合并为一个,只更新其计数或内容。
Laravel的通知系统提供了一个便捷的方式来向用户发送各类通知。对于数据库通知,我们通常会在通知类的 toDatabase 方法中定义要存储到 notifications 表中的数据。当 Notification::send() 方法被调用时,Laravel会调用相应渠道(如 toDatabase)的方法,并将其返回的数据存储起来。
toDatabase 方法的典型结构如下:
public function toDatabase($notifiable): array
{
return [
'content' => '这是一条新通知',
'type' => 'post_created',
'post_id' => $this->post->id,
];
}Laravel会将这个返回的数组存储到 notifications 表的 data 字段中。
我们的核心策略是:当一个事件触发通知时,首先检查在预设的时间窗口(例如30分钟)内,是否已经存在一个针对相同主题的通知。
为了实现上述策略,我们需要修改通知类中的 toDatabase 方法。原始的问题代码尝试在找到现有通知后进行更新,但最终仍然会执行方法末尾的 return [] 语句,导致即使更新了旧通知,也会创建一条新通知。
关键在于:当成功更新现有通知后,必须阻止 toDatabase 方法继续返回一个非空的数组,因为任何非空数组都会被Laravel视为一条新的通知记录来存储。 返回一个空数组 [] 或 null 是阻止新通知创建的有效方式。
下面是优化后的 toDatabase 方法示例:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\DatabaseMessage;
use App\Models\Search; // 假设您的Search模型
class PostMatchedSearchNotification extends Notification
{
use Queueable;
protected Search $search;
protected string $postTitle; // 假设通知中需要展示的帖子标题
/**
* 创建一个新的通知实例。
*
* @param Search $search 触发通知的搜索条件
* @param string $postTitle 匹配到的帖子标题
* @return void
*/
public function __construct(Search $search, string $postTitle = '新帖子')
{
$this->search = $search;
$this->postTitle = $postTitle;
}
/**
* 获取通知的交付渠道。
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable): array
{
return ['database'];
}
/**
* 将通知转换为数据库存储的数组。
*
* @param mixed $notifiable
* @return array
*/
public function toDatabase($notifiable): array
{
$timeWindowMinutes = 30; // 定义时间窗口:30分钟
$searchIdentifier = $this->search->id; // 用于识别相关通知的唯一标识
// 尝试查找在指定时间窗口内,针对当前搜索条件已存在的通知
$existingNotification = $notifiable->notifications()
->where('data->search_id', $searchIdentifier) // 使用 data->search_id 进行查询
->where('created_at', '>=', now()->subMinutes($timeWindowMinutes))
->first();
if ($existingNotification) {
// 如果找到了现有通知,则更新它
$currentCount = $existingNotification->data['count'] ?? 0;
$newCount = $currentCount + 1;
$existingNotification->update([
'data' => array_merge($existingNotification->data, [
'content' => [
'en' => "{$newCount} new posts matched with your saved search '{$this->search->title}' have been posted. Click to view more.",
'zh' => "您的保存搜索 '{$this->search->title}' 匹配到 {$newCount} 篇新帖子。点击查看更多。",
],
'count' => $newCount,
// 可以更新其他相关数据,例如最后更新时间
'last_updated_at' => now()->toDateTimeString(),
// 确保 search_id 和 parameters 等关键标识符仍然存在
'search_id' => $searchIdentifier,
'parameters' => $this->search->parameters,
])
]);
// 关键:返回一个空数组,阻止Laravel创建新的通知记录
return [];
}
// 如果在时间窗口内没有找到现有通知,则创建一条新的通知
return [
'content' => [
'en' => "1 new post matched with your saved search '{$this->search->title}' has been posted. Click to view more.",
'zh' => "您的保存搜索 '{$this->search->title}' 匹配到 1 篇新帖子。点击查看更多。",
],
'count' => 1,
'search_id' => $searchIdentifier, // 用于后续查询的标识符
'parameters' => $this->search->parameters,
'first_matched_post_title' => $this->postTitle, // 首次匹配到的帖子标题
'created_at' => now()->toDateTimeString(), // 记录创建时间
];
}
}代码解析:
created_at 与 updated_at 的选择:
data 字段的设计:
性能考量:
用户体验:
其他通知渠道:
通过在Laravel通知类的 toDatabase 方法中引入条件判断和早期返回机制,我们可以有效地实现数据库通知的智能合并与去重。这不仅能够显著减少用户收到的通知数量,避免“通知疲劳”,还能提升整体的用户体验。合理设计 data 字段结构,并结合索引优化,可以确保此方案在不同规模的应用中都能高效运行。
以上就是Laravel数据库通知的智能管理:实现通知合并与去重的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号