zero2one / snooze
在 Laravel 中安排未来的通知和提醒
Requires
- php: >=7.2.5
- illuminate/support: ~6.0 || ~7.0 || ~8.0
Requires (Dev)
- orchestra/testbench: ~4.0|| ~5.0 || ~6.0
- phpunit/phpunit: ^9.0
- timacdonald/log-fake: ^1.7
This package is auto-updated.
Last update: 2024-09-10 17:42:22 UTC
README
在 Laravel 中安排未来的通知和提醒
为什么使用这个包?
- 你是否想过在特定时间发送一个 未来的 通知?(延迟队列选项是否不够用?)
- 你想要一个简单的用户入门邮件序列吗?
- 或者生日快乐邮件呢?
常见用例
- 提醒系统(预约前一周,前一天,一小时前等)
- 跟进调查(购买后两天)
- 用户入门邮件序列(注册后欢迎邮件,3天后提供额外提示,7天后提供升级优惠)
- 短期周期性报告(下周四周内每周发送一次)
安装
通过 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
来测试它。 要自动运行它,请参阅这里。
设置发送容差
如果您的调度程序停止工作,计划中的通知将积压。为了防止用户一次性收到所有旧的计划通知,该命令将仅在配置的容差内发送邮件。默认情况下,这设置为 24 小时,因此只有计划在此窗口内发送的邮件才会发送。这可以通过 SCHEDULED_NOTIFICATION_SEND_TOLERANCE
环境变量或 snooze.php
配置文件中的 snooze.php
配置文件进行配置。
设置修剪年龄
该包可以修剪 x 天前发送/取消发送的已发送和已取消的消息。您可以使用 SCHEDULED_NOTIFICATION_PRUNE_AGE
环境变量或 snooze.php
配置文件中的 SCHEDULED_NOTIFICATION_PRUNE_AGE
配置文件进行配置(单位是天)。默认情况下,此功能是关闭的。
详细示例
取消计划中的通知
$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()
方法添加到任何通知中。此方法将在发送通知之前立即进行检查。
例如,如果用户变得不活跃,或者通知所针对的订单已被取消,您可能不会发送未来的 drip 通知。
public function shouldInterrupt($notifiable) { return $notifiable->isInactive() || $this->order->isCanceled(); }
如果您的通知中没有此方法,通知将不会被中断。如果您希望对一组通知重复使用条件逻辑,请考虑创建一个 shouldInterupt 特性。
计划通知元信息
可以在计划通知中存储元信息,然后在稍后阶段通过此元信息查询计划通知。
此功能在您为未来日期存储通知,但系统中的某些更改需要您更新它们时可能很有用。通过使用元列,可以更容易地通过不是可通知的内容来查询数据库中的这些计划通知。
存储元信息
使用 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
安全
如果您发现任何安全问题,请通过电子邮件联系,而不是使用问题跟踪器。
贡献
- Fork 它 (https://github.com/thomasjohnkane/snooze/fork)
- 创建您的功能分支 (
git checkout -b feature/fooBar
) - 提交您的更改 (
git commit -am 'Add some fooBar'
) - 将更改推送到分支 (
git push origin feature/fooBar
) - 创建一个新的 Pull Request
鸣谢
此软件包是在 melihovv/laravel-package-generator 的帮助下启动的。