Канал push
Канал push относится к компоненту
Notifier и отвечает за отправку
уведомлений через push-сервисы. Канал
подключается в методе channels
класса уведомления. После этого
Notifier передаёт сообщение
транспорту push, указанному в конфигурации.
Синтаксис
public function channels(RecipientInterface $recipient): array
Пример
Давайте подключим канал push
в классе уведомления:
<?php
namespace AppNotification;
use SymfonyComponentNotifierNotificationNotification;
use SymfonyComponentNotifierRecipientRecipientInterface;
class ArticleNotification extends Notification
{
public function __construct(
private string $text,
) {
}
public function getContent(): string
{
return $this->text;
}
public function getChannels(RecipientInterface $recipient): array
{
return ['push'];
}
}
?>
Пример
Давайте отправим уведомление через
Notifier:
<?php
namespace AppController;
use AppNotificationArticleNotification;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentNotifierNotifierInterface;
use SymfonyComponentNotifierRecipientRecipient;
use SymfonyComponentRoutingAttributeRoute;
class ArticleController extends AbstractController
{
#[Route('/article', name: 'article_index')]
public function index(NotifierInterface $notifier): Response
{
$recipient = new Recipient('user@example.com');
$notification = new ArticleNotification('hello');
$notifier->send($notification, $recipient);
return new Response('sent');
}
}
?>
Результат выполнения кода:
"sent"
Пример
Давайте проверим список каналов уведомления:
<?php
namespace AppController;
use AppNotificationArticleNotification;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentNotifierRecipientRecipient;
use SymfonyComponentRoutingAttributeRoute;
class ArticleController extends AbstractController
{
#[Route('/channels', name: 'article_channels')]
public function channels(): Response
{
$recipient = new Recipient('user@example.com');
$notification = new ArticleNotification('abcde');
$res = $notification->getChannels($recipient);
return new Response(json_encode($res));
}
}
?>
Результат выполнения кода:
["push"]