php 站内消息通知系统,Laravel 的消息通知系统

1.生成数据库

$ php artisan notifications:table

$ php artisan migrate

生成的表是notifications 数据结构是固定的

2.应用-生成话题回复通知类

$ php artisan make:notification TopicReplied

3. 触发通知

写在模型监控器里 --app/Observers/ReplyObserver.php

.

.

.

use App\Notifications\TopicReplied;

class ReplyObserver

{

public function created(Reply $reply)

{

$topic = $reply->topic;

$topic->increment('reply_count', 1);

// 通知作者话题被回复了

$topic->user->notify(new TopicReplied($reply));

}

.

.

.

}

app/Models/User.php

.

.

.

use Auth;

class User extends Authenticatable

{

use Notifiable {

// Notifiable 的trait里面有notify方法,此处我们给它个别名

notify as protected laravelNotify;

}

public function notify($instance)

{

// 如果要通知的人是当前用户,就不必通知了!

if ($this->id == Auth::id()) {

return;

}

$this->increment('notification_count'); //字段加1

$this->laravelNotify($instance); // 发送通知

}

.

.

.

}

其实我们引用的是RoutesNotifications类的notify方法

public function notify($instance)

{

app(Dispatcher::class)->send($this, $instance);

}

如果直接使用

Notification::send($users,new TopicReplied($reply));

框架内获取通知内容的trait文件

namespace Illuminate\Notifications;

trait HasDatabaseNotifications

{

/**

* Get the entity's notifications.

*/

public function notifications()

{

return $this->morphMany(DatabaseNotification::class, 'notifiable')

->orderBy('created_at', 'desc');

}

/**

* Get the entity's read notifications.

*/

public function readNotifications()

{

return $this->notifications()

->whereNotNull('read_at');

}

/**

* Get the entity's unread notifications.

*/

public function unreadNotifications()

{

return $this->notifications()

->whereNull('read_at');

}

}

测试获取数据

$user = User::find(1);

$test = $user->notifications;

dd($test->toArray());

已读未读是有一个 read_at 字段

$user->unreadNotifications; // 获取所有未读通知

$user->readNotifications; // 获取所有已读通知

$user->notifications; // 获取所有通知

dcc85aa3b9c9

图片.png

4. 清除未读消息标示

.

.

.

class User extends Authenticatable

{

.

.

.

public function markAsRead()

{

$this->notification_count = 0;

$this->save();

$this->unreadNotifications->markAsRead();

}

}

使用

修改控制器的 index() 方法,新增清空未读提醒的状态:

app/Http/Controllers/NotificationsController.php

.

.

.

class NotificationsController extends Controller

{

.

.

.

public function index()

{

// 获取登录用户的所有通知

$notifications = Auth::user()->notifications()->paginate(20);

// 标记为已读,未读数量清零

Auth::user()->markAsRead();

return view('notifications.index', compact('notifications'));

}

}

这个handle方法就是我们要做的具体实现了,有个很方便的功能就是如果implements ShouldQueue这个接口的话就会异步队列执行,如果去掉的话就是同步执行。

注册的时候其实建议使用这种方式

namespace App\Providers;

use Laravel\Lumen\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider

{

/**

* The event listener mappings for the application.

*

* @var array

*/

protected $listen = [

// 用户注册后的事件

'App\Events\Register' => [

// 发送广告邮件

'App\Listeners\SendAdMail',

// 发送短信

'App\Listeners\SendSms',

// 发送帮助信息

'App\Listeners\SendHelpInformation',

],

];

}

然后执行执行php artisan event:generater 直接会在相应的目录生成相应的这四个类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值