friendsofhyperf / notification
The Notification for Hyperf.
Fund package maintenance!
huangdijia
hdj.me/sponsors
Requires
- hyperf/collection: ~3.1.0
- hyperf/di: ~3.1.0
- hyperf/event: ~3.1.0
- hyperf/stringable: ~3.1.0
- hyperf/translation: ~3.1.0
Suggests
- hyperf/database: Required if you want to use database channel.(~3.1.0)
- overtrue/easy-sms: Required if you want to use sms channel.(^3.0)
- symfony/notifier: Required if you want to use symfony notifier channel.(^6.4 | ^7.0)
This package is auto-updated.
Last update: 2024-10-25 03:51:24 UTC
README
Installation
composer require friendsofhyperf/notification:~3.1.0
Usage
Model use Notifiable
trait
<?php declare(strict_types=1); namespace App\Model; use Hyperf\DbConnection\Model\Model; use FriendsOfHyperf\Notification\Traits\Notifiable;use Overtrue\EasySms\PhoneNumber; /** * @property int $id * @property \Carbon\Carbon $created_at * @property \Carbon\Carbon $updated_at */ class User extends Model { use Notifiable; /** * The table associated with the model. */ protected ?string $table = 'user'; /** * The attributes that are mass assignable. */ protected array $fillable = ['id', 'created_at', 'updated_at']; /** * The attributes that should be cast to native types. */ protected array $casts = ['id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime']; // 通知手机号 public function routeNotificationForSms(): string|PhoneNumber { return $this->phone; } }
Database Notifications
# Install the database package composer require hyperf/database:~3.1.0 # Publish the migration file php bin/hyperf.php notification:table # Run the migration php bin/hyperf.php migrate # Create a notification php bin/hyperf.php make:notification TestNotification
<?php namespace App\Notification; use FriendsOfHyperf\Notification\Notification; class TestNotification extends Notification { public function __construct( private string $message ){} public function via() { return [ // database channel 'database' ]; } public function toDatabase(mixed $notifiable): array { return [ 'message' => $this->message, ]; } }
// Your controller or service // 通知一条消息 $user->notify(new TestNotification('系统通知:xxx')); $noReadCount = $user->unreadNotifications()->count(); $this->output->success('发送成功,未读消息数:' . $noReadCount); $notifications = $user->unreadNotifications()->first(); $this->output->success('消息内容:' . $notifications->data['message']); $notifications->markAsRead(); $noReadCount = $user->unreadNotifications()->count(); $this->output->success('标记已读,未读消息数:' . $noReadCount);
SMS Notifications
Symfony Notifications
Send notifications using Symfony Notifier.
Email, SMS, Slack, Telegram, etc.
composer require symfony/notifier
composer require symfony/mailer
<?php // app/Factory/Notifier.php namespace App\Factory; use Hyperf\Contract\StdoutLoggerInterface; use Psr\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Mailer\Transport; use Symfony\Component\Notifier\Channel\ChannelInterface; use Symfony\Component\Notifier\Channel\EmailChannel; use function Hyperf\Support\env; class Notifier { public function __construct( protected EventDispatcherInterface $dispatcher, protected StdoutLoggerInterface $logger, ) { } public function __invoke() { return new \Symfony\Component\Notifier\Notifier($this->channels()); } /** * @return ChannelInterface[] */ public function channels(): array { return [ 'email' => new EmailChannel( transport: Transport::fromDsn( // MAIL_DSN=smtp://user:password@localhost:1025 env('MAIL_DSN'), dispatcher: $this->dispatcher, logger: $this->logger ), from: 'root@imoi.cn' ), ]; } }
<?php // app/Notification/TestNotification.php namespace App\Notification; use App\Model\User; use FriendsOfHyperf\Notification\Notification; use Symfony\Component\Notifier\Recipient\Recipient; class TestNotification extends Notification { public function __construct( private string $message ){} public function via() { return [ 'symfony' ]; } public function toSymfony(User $user) { return (new \Symfony\Component\Notifier\Notification\Notification($this->message,['email']))->content('The introduction to the notification.'); } public function toRecipient(User $user) { return new Recipient('2771717608@qq.com'); } }
<?php // config/autoload/dependencies.php declare(strict_types=1); /** * This file is part of Hyperf. * * @link https://www.hyperf.io * @document https://hyperf.wiki * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ return [ \Symfony\Component\Notifier\NotifierInterface::class => \App\Factory\Notifier::class ];
Usage in controller
$user = User::create(); // 通知一条消息 $user->notify(new TestNotification('系统通知:xxx'));