消息通知提醒 laravel notification

更新日期: 2019-05-08 阅读次数: 8989 字数: 319 分类: Laravel

参考 https://laravel.com/docs/5.5/notifications

创建提醒

php artisan make:notification InvoicePaid

app/Notifications 目录下会新增一个对应的 class

场景

  • 关注的人有发新帖子
  • 自己的帖子有人回复
  • 自己的回复有人回复

单独发送消息 VS 群发消息

给指定用户发送

$user->notify(new InvoicePaid($invoice));

群发消息

Notification::send($users, new InvoicePaid($invoice));

通知的方式

  • 邮件。自带的邮件模板非常给力。我觉得有必要尝试一下。
  • 短信
  • 聊天软件消息。例如,Slack
  • 存数据库
  • 等等

通过 via 函数指定。

Database Notifications

建表

php artisan notifications:table
php artisan migrate

标记为已读

You may also use a mass-update query to mark all of the notifications as read without retrieving them from the database:

$user = App\User::find(1);
$user->unreadNotifications()->update(['read_at' => now()]);

消息队列

当单个事件触发的消息量巨大,例如,大 V 发twitter,大量粉丝需要接收消息。或者,消息需要调用三方 API 时,就需要用到队列。

我看 Laravel 支持的不错,能节省很大的工作量。

但是最终我还是选择自己实现

没有使用 Laravel 内置 Notification 的原因

  • 我的使用场景非常单一,只需要提供未读消息即可
  • 在数据量大时,自己的实现方案更可控

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式