patienceman / synca
提供与通知(包括电子邮件、Dbnotification 和 OneSignal 通知)交互的便捷方式,这使您的声明和交互更加容易。
Requires
- php: ^7.4|^8.0
- kakaprodo/custom-data: ^2.0
README
提供与通知(包括电子邮件、Dbnotification 和 OneSignal 通知)交互的便捷方式,这使您的声明和交互更加容易。
安装
安装此包不需要太多要求,只需在 Laravel 终端中运行以下命令即可。
composer require patienceman/synca
在开始之前,首先创建 Queue 表和 Notifications。
php artisan queue:table php artisan notification:table php artisan migrate最后,别忘了更新您的应用程序以使用数据库
driver
,通过更新应用程序的env
或任何您使用的队列驱动中的QUEUE_CONNECTION
变量,考虑查阅 Laravel 队列文档。QUEUE_CONNECTION=database
用法
要开始使用 Noptifier,您需要在自定义目录中运行 🎉 命令。
php artisan make:notifier EmailNotification
这样它就会为您创建筛选文件,只需在 Notifiers 目录中即可。
App\Notifiers
但在这份文档中,我们将使用
App\Notifications
namespace App\Notifications; use Patienceman\Synca\NotifyHandler; class EmailNotification extends NotifyHandler { /** * Execute notification actions * * @return mixed */ public function handle() { // do whatever action inside handler } }
因此,您可能需要指定 Notifier 的自定义路径,只需放松并在 Notifier 名称前添加即可。让我们再次以当前示例为例。
php artisan make:notifier Model/EmailNotification
为了与 Notifier 通信/使用,您只需调用 Notifier 类。让我们在 CandidateController 类中快速举例说明如何通知创建者与寻求者之间的申请。
namespace App\Http\Controllers; use App\Notifications\EmailNotification; use Patienceman\Synca\Facades\Notifier; class UsersController extends Controller { /** * Handle User Notifications */ public function notifications(Notifier $notifier) { // ... Other Codes $notifier->handle([ EmailNotification::process([ 'message' => 'Application sent to job sent' ]), ]); } }
现在,我们能够随时随地向任何地方发送电子邮件通知。因此,Notifier 有很多功能,包括
->onQueue()
->to($user1, $user2, ....)
让我们看一下
namespace App\Http\Controllers; use App\Notifications\EmailNotification; use Patienceman\Synca\Facades\Notifier; class UsersController extends Controller { /** * Handle User Notifications */ public function notifications(Notifier $notifier, User $user) { // ... Other Codes $application = Application::findById('1')->belongsToCompany()->user_id; $notification = [ 'message' => 'Application sent to job sent' ]; $users = [ 'user' => $user 'applicant' => $application ]; $notifier->handle([ EmailNotification::process($notification) ->to($users) ->onQueue(), ]); } }
要访问传递的用户,您只需依次调用它们:例如,使用
->to($users);
$this->user; $this->applicant;
这很酷,但可能有时您需要将所有 Notifier 队列化,而不是像上面那样单个化,让我们看看如何:但我们也有 OneSignalNotification
namespace App\Http\Controllers; use App\Notifications\EmailNotification; use App\Notifications\OneSignalNotification; use Patienceman\Synca\Facades\Notifier; class UsersController extends Controller { public function notifications(Notifier $notifier, User $user) { $application = Application::findById('1')->belongsToCompany()->user_id; $notification = [ 'message' => 'Application sent to job sent' ]; $users = [ 'user' => $user 'applicant' => $application ]; $notifier->handle([ EmailNotification::process($notification)->to($users), OneSignalNotification::process($notification)->to([$user]), ])->onQueue(); } }
如您所见,我们正在使用有效载荷与 Notifier 一起工作,让我们看看如何获取所有有效载荷和所有目标用户。
namespace App\Notifications; use Patienceman\Synca\NotifyHandler; class EmailNotification extends NotifyHandler { /** * Execute notification actions * @return mixed */ public function handle() { $this->message; // this will get single passed payload $this->payload(); // this will get all payload as object $this->recipients(); // this will get all targeted users } }
还有,如果您想向所有收件人发送通知而不选择特定的人,只需使用函数即可
$this->foreachUser()
$this->foreachUser(fn($user) => $this->sendToDatabase($user));
您已经掌握了这个函数,这个函数可以用于 Laravel DBNotification 以在表中存储自定义通知,让我们看看完整的实现
namespace App\Notifications; use Patienceman\Synca\NotifyHandler; class DatabaseNotification extends NotifyHandler { /** * Execute notification * @return mixed */ public function handle() { $this->foreachUser( fn($user) => $this->sendToDatabase($user, $this) ); } /** * Get the array to database representation of the notification. * * @param mixed $notifiable * @return array */ public function toDatabase($notifiable) { return $this->payloadAsArray(); } }
或使用自定义方式
namespace App\Notifications; use Patienceman\Synca\NotifyHandler; class DatabaseNotification extends NotifyHandler { /** * Execute notification * @return mixed */ public function handle() { $this->foreachUser(function($user) { $this->dbNotification($user, fn ($notifiable) => [ 'header' => $this->subject, 'message' => $this->message, 'action' => $this->action, ]); }); } }
贡献
欢迎拉取请求。对于重大更改,请首先打开一个问题以讨论您想要更改的内容。请确保根据需要更新测试。