thomasjohnkane / laravel-snooze
Requires
- php: ^8.1
- illuminate/support: ^10.0 | ^11.0
Requires (Dev)
- orchestra/testbench: ^8.0 | ^9.0
- phpunit/phpunit: ^10.0 | ^11.0
- timacdonald/log-fake: ^2
This package is auto-updated.
Last update: 2024-04-16 01:53:40 UTC
README
在Laravel中安排未来的通知和提醒
为什么使用这个包?
- 是否想要安排一个在特定时间发送的未来通知?(延迟队列选项不够吗?)
- 想要一个简单的入职邮件漏斗吗?
- 如何处理生日电子邮件?
常见用例
- 提醒系统(预约前一周,前一天,一小时前等)
- 跟进调查(购买后2天)
- 入职邮件漏斗(注册后欢迎邮件,3天后额外提示,7天后升级优惠)
- 短期重复报告(下周4周内每周发送一次)
安装
通过composer安装
composer require thomasjohnkane/snooze
php artisan migrate
发布配置文件
php artisan vendor:publish --provider="Thomasjohnkane\Snooze\ServiceProvider" --tag="config"
使用方法
使用模型特质
Snooze为您的模型提供了一个特质,类似于标准的Notifiable
特质。它为您的模型添加了一个notifyAt()
方法来安排通知。
use Thomasjohnkane\Snooze\Traits\SnoozeNotifiable; use Illuminate\Notifications\Notifiable; class User extends Model { use Notifiable, SnoozeNotifiable; // ... } // Schedule a birthday notification $user->notifyAt(new BirthdayNotification, Carbon::parse($user->birthday)); // Schedule for a week from now $user->notifyAt(new NextWeekNotification, Carbon::now()->addDays(7)); // Schedule for new years eve $user->notifyAt(new NewYearNotification, Carbon::parse('last day of this year'));
使用ScheduledNotification::create辅助函数
您还可以使用ScheduledNotification
上的create
方法。
ScheduledNotification::create( Auth::user(), // Target new ScheduledNotificationExample($order), // Notification Carbon::now()->addHour() // Send At );
这也有助于安排匿名通知(直接路由,而不是在模型上)。
$target = (new AnonymousNotifiable) ->route('mail', 'hello@example.com') ->route('sms', '56546456566'); ScheduledNotification::create( $target, // Target new ScheduledNotificationExample($order), // Notification Carbon::now()->addDay() // Send At );
关于安排snooze:send命令的重要说明
创建一个计划通知将把通知添加到数据库中。它将在运行snooze:send
命令时(或之后)发送,该命令的时间是存储的sendAt
时间。
snooze:send
命令默认每分钟运行一次。您可以在发布的配置文件中更改此值(sendFrequency
)。可用选项有everyMinute
、everyFiveMinutes
、everyTenMinutes
、everyFifteenMinutes
、everyThirtyMinutes
、hourly
和daily
。
您需要做的唯一一件事是确保schedule:run
也在运行。您可以在控制台中运行php artisan schedule:run
来测试这一点。要使其自动运行,请在此处阅读。
注意:如果您希望snooze不自动安排命令,可以将
scheduleCommands
配置值设置为false
。
设置发送容差
如果您的调度器停止工作,计划通知的积压将不断增长。为了防止用户一次性收到所有旧的计划通知,命令将只在配置的容差内发送邮件。默认情况下,这设置为24小时,因此只有在此窗口内计划发送的邮件才会发送。这可以通过SCHEDULED_NOTIFICATION_SEND_TOLERANCE
环境变量或snooze.php
配置文件进行配置。
设置修剪年龄
此包可以修剪x天前发送/取消发送的已发送和已取消的消息。您可以使用SCHEDULED_NOTIFICATION_PRUNE_AGE
环境变量或snooze.php
配置文件进行配置(单位为天)。默认情况下,此功能是关闭的。
详细示例
取消计划通知
$notification->cancel();
注意:您无法取消已发送的通知。
重新安排计划通知
$rescheduleAt = Carbon::now()->addDay(1) $notification->reschedule($rescheduleAt)
注意:无法重新安排已发送或取消的通知。如果您想复制已发送或取消的通知,请与新的发送日期一起传递一个真值第二个参数;reschedule($date, true)
,或使用下面的scheduleAgainAt($date)
方法。
将计划发送的通知重复发送
$notification->scheduleAgainAt($newDate); // Returns the new (duplicated) $notification
检查计划通知的状态
// Check if a notification is already cancelled $result = $notification->isCancelled(); // returns a bool // Check if a notification is already sent $result = $notification->isSent(); // returns a bool
有条件地中断计划通知
如果您想有条件地停止发送电子邮件,可以将shouldInterrupt()
方法添加到任何通知中。此方法将在通知发送前立即进行检查。
例如,如果用户变得不活跃,或者通知所针对的订单已被取消,则可能不会发送未来的定时通知。
public function shouldInterrupt($notifiable) { return $notifiable->isInactive() || $this->order->isCanceled(); }
如果此方法不在您的通知中,则通知将不会中断。如果您想对一组通知重复条件逻辑,请考虑创建一个shouldInterupt特性。
计划通知元信息
可以在计划通知上存储元信息,然后在稍后阶段通过这些元信息查询计划通知。
此功能在存储未来的通知时非常有用,但系统中的某些更改需要您更新它们。通过使用元列,可以通过除notifiable之外的内容更轻松地从数据库中查询这些计划通知。
存储元信息
使用ScheduledNotification::create
辅助函数
ScheduledNotification::create( $target, // Target new ScheduledNotificationExample($order), // Notification Carbon::now()->addDay(), // Send At, ['foo' => 'bar'] // Meta Information );
使用notifyAt
特性
$user->notifyAt(new BirthdayNotification, Carbon::parse($user->birthday), ['foo' => 'bar']);
从计划通知中检索元信息
您可以在现有计划通知上调用getMeta
函数来检索特定通知的元信息。
向此函数传递没有参数将返回整个元列的数组形式。
传递一个字符串键(getMeta('foo')
),将检索元列中的特定键。
使用ScheduledNotification::findByMeta
辅助函数查询计划通知
您可以使用findByMeta
辅助函数通过数据库查询具有特定元信息的计划通知。
ScheduledNotification::findByMeta('foo', 'bar'); //key and value
第一个参数是元键,第二个参数是要查找的值。
注意:索引列目前没有使用数据库索引
有条件地关闭调度器
如果您想禁用计划通知的发送,请设置环境变量SCHEDULED_NOTIFICATIONS_DISABLED
为true
。您仍然可以安排通知,一旦调度器启用,它们将发送。
这可以用于确保计划通知只由特定的服务器发送,例如。
启用onOneServer
如果您想使snooze
命令利用Laravel调度器的onOneServer
功能,可以使用以下环境变量
SCHEDULED_NOTIFICATIONS_ONE_SERVER = true
运行测试
composer test
安全性
如果您发现任何安全问题,请通过电子邮件而不是使用问题跟踪器。
贡献
- 分支它(https://github.com/thomasjohnkane/snooze/fork)
- 创建您的功能分支(
git checkout -b feature/fooBar
) - 提交您的更改(
git commit -am 'Add some fooBar'
) - 推送到分支(
git push origin feature/fooBar
) - 创建一个新的拉取请求
致谢
此包是在melihovv/laravel-package-generator的帮助下启动的。