PHP code example of friendsofhyperf / notification

1. Go to this page and download the library: Download friendsofhyperf/notification library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

friendsofhyperf / notification example snippets




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;
    }
}



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);


// 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: '[email protected]'
            ),
        ];
    }
}


// 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('[email protected]');
    }


}


// config/autoload/dependencies.php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  [email protected]
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
return [
    \Symfony\Component\Notifier\NotifierInterface::class => \App\Factory\Notifier::class
];


$user = User::create();
// 通知一条消息
$user->notify(new TestNotification('系统通知:xxx'));
shell
# Install the database package
composer .php notification:table

# Run the migration
php bin/hyperf.php migrate

# Create a notification
php bin/hyperf.php make:notification TestNotification