laravel-notification-channels-ru-store maintained by yakoffka
Please see this repo for instructions on how to submit a channel proposal.
This package makes it easy to send notifications using [RuStore](link to service) with Laravel .
Contents
- Requirements
- Installation
- Setting up the RuStore service
- Usage
- Available Message methods
- Changelog
- Testing
- Security
- Contributing
- Credits
- License
Requirements
- PHP >= 8.2
- Laravel / Illuminate >= 11.0
Installation
Установите пакет с помощью команды:
composer require yakoffka/laravel-notification-channels-ru-store
Опубликуйте конфигурационный файл:
php artisan vendor:publish --provider="NotificationChannels\RuStore\RuStoreServiceProvider"
Обновите ваш .env, указав там значения, полученные в RuStore консоли
Usage
В классе, использующим трейт Notifiable (например User), необходимо реализовать метод, возвращающий массив токенов уведомляемого пользователя:
/**
* Получение массива ru-store push-токенов пользователя.
* Используется пакетом yakoffka/laravel-notification-channels-ru-store (laravel-notification-channels/rustore)
*
* @return array<int|string, mixed>
*/
public function routeNotificationForRuStore(): array
{
return $this->rustore_push_tokens;
}
Затем создать класс уведомления, в методе via() которого указать канал отправки RuStoreChannel и добавить метод toRuStore():
<?php
declare(strict_types=1);
namespace App\Notifications\Development;
use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use NotificationChannels\RuStore\Resources\MessageAndroid;
use NotificationChannels\RuStore\Resources\MessageAndroidNotification;
use NotificationChannels\RuStore\Resources\MessageNotification;
use NotificationChannels\RuStore\RuStoreChannel;
use NotificationChannels\RuStore\RuStoreMessage;
/**
* Уведомление пользователя, отправляемое через консоль для проверки работы канала RuStore
*/
class RuStoreTestNotification extends Notification implements ShouldQueue
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(public readonly string $title, public readonly string $body)
{
}
/**
* Get the notification's delivery channels.
*
* @param User $notifiable
* @return array
*/
public function via(User $notifiable): array
{
return [
RuStoreChannel::class, // указать канал отправки RuStoreChannel
];
}
/**
* Формирование сообщения, отправляемого через RuStoreChannel
*
* @param User $notifiable
* @return RuStoreMessage
*/
public function toRuStore(User $notifiable): RuStoreMessage
{
return (new RuStoreMessage(
notification: new MessageNotification(
title: 'Test Push by RuStore',
body: 'Hello! Test body from RuStoreTestingNotification',
),
android: new MessageAndroid(
notification: new MessageAndroidNotification(
title: 'Android test Push by RuStore',
body: 'Hello! Android test body from RuStoreTestingNotification',
)
)
));
}
}
Проверка отправки уведомлений
Для контроля отправляемых уведомлений необходимо воспользоваться событиями, поджигаемыми после отправки:
- cобытие NotificationSent содержит коллекцию отчетов RuStoreReport в свойстве response:
$report = $event->response; - cобытие NotificationFailed содержит единичный отчет RuStoreReport в свойстве data['report']:
$report = Arr::get($event->data, 'report');
Отчет RuStoreReport имеет публичный метод target(), возвращающий значение ru-store push токена, на который производилась отправка.
Обработка события успешной отправки
Пример использования события NotificationSent:
// class SentListener
/**
* Обработка успешно отправленных сообщений
*/
public function handle(NotificationSent $event): void
{
match ($event->channel) {
RuStoreChannel::class => $this->handleRuStoreSuccess($event),
default => null
};
}
/**
* Логирование успешно отправленных ru-store-уведомлений
*/
public function handleRuStoreSuccess(NotificationSent $event): void
{
/** @var Collection<string, RuStoreReport> $reports */
$reports = $event->response;
$tokens = $reports->map(fn(RuStoreReport $singleReport) => $singleReport->target());
if ($tokens->isNotEmpty()) {
/** @var NotifiableUser $notifiable */
$notifiable = $event->notifiable;
Log::channel('notifications_ru_store')->info("Успешно отправлены уведомления {$notifiable->short_info}", [
'tokens' => $tokens->toArray(),
]);
}
}
NOTE: Событие NotificationSent поджигается всегда, даже в случае отсутствия успешно отправленных сообщений. Индикатором успешной отправки является непустая коллекция отчетов.
Обработка события неуспешной отправки
Пример использования события NotificationFailed:
// class FailedSendingListener
public function handle(NotificationFailed $event): void
{
match ($event->channel) {
RuStoreChannel::class => $this->handleRuStoreFailed($event),
default => null
};
}
/**
* Обработка неудачной отправки ru-store-уведомлений
*
* @param NotificationFailed $event
* @return void
*/
private function handleRuStoreFailed(NotificationFailed $event): void
{
/** @var RuStoreReport $report */
$report = Arr::get($event->data, 'report'); // @todo $report может быть null: падение до отправки!
/** @var Throwable $e */
$e = $report->error();
/** @var NotifiableUser $notifiable */
$notifiable = $event->notifiable;
Log::channel('notifications_ru_store')
->error("Ошибка отправки уведомления {$notifiable->short_info} " . $e::class, compact('event'));
if ($e::class === NotFoundException::class || $e::class === InvalidPushTokenException::class) {
resolve(RuStorePushService::class)->deleteRuStoreToken($notifiable, $report->target(), $e);
}
}
NOTE: Событие NotificationFailed поджигается только в случае наличия хотя-бы одной неуспешной отправки. NOTE: В случае, если было выброшено исключение NotFoundException или InvalidPushTokenException, необходимо отозвать невалидный/недействующий токен.
Available Message methods
Сообщение RuStoreMessage поддерживает все свойства, описанные в документации rustore
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Security
If you discover any security related issues, please email yagithub@mail.ru instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.