yakoffka/laravel-notification-channels-ru-store

RuStore push notifications Driver for Laravel

1.0.1 2025-05-07 14:16 UTC

This package is auto-updated.

Last update: 2025-06-14 05:47:46 UTC


README

Please see this repo for instructions on how to submit a channel proposal.

Software License

This package makes it easy to send notifications using [RuStore](link to service) with Laravel 10.x.

Contents

Installation

Установите пакет с помощью команды:

  composer require yakoffka/laravel-notification-channels-ru-store

Затем опубликуйте конфигурационный файл:

  php artisan vendor:publish --provider="NotificationChannels\RuStore\RuStoreServiceProvider"

и обновите ваш .env, указав там значения, полученные в RuStore консоли

Setting up the RuStore service

Optionally include a few steps how users can set up the service.

Usage

В классе, использующим трейт Notifiable (например User), необходимо реализовать метод, возвращающий массив токенов уведомляемого пользователя:

    /**
     * Получение массива ru-store пуш-токенов, полученных пользователем.
     * Используется пакетом laravel-notification-channels/rustore
     *
     * @return array
     */
    public function routeNotificationForRuStore(): array
    {
        return $this->ru_store_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::all() вернет коллекцию отчетов RuStoreSingleReport об отправке уведомлений на конкретное устройство с 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 RuStoreReport $report */
        $report = $event->response;

        $report->all()->each(function (RuStoreSingleReport $singleReport, string $token) use ($report, $event): void {
            /** @var Response $response */
            $response = $singleReport->response();
            Log::channel('notifications')->info('RuStoreSuccess Уведомление успешно отправлено', [
                'user' => $event->notifiable->short_info,
                'token' => $token,
                'message' => $report->getMessage()->toArray(),
                'response_status' => $response->status(),
            ]);
        });
    }

NOTE: Событие NotificationSent поджигается только в случае наличия успешно отправленных сообщений.

Пример использования события NotificationFailed:

    // class FailedSendingListener

    public function handle(NotificationFailed $event): void
    {
        match ($event->channel) {
            RuStoreChannel::class => $this->handleRuStoreFailed($event),
            default => null
        };
    }

    /**
     * Обработка неудачных отправок уведомлений через канал RuStore
     *
     * @param NotificationFailed $event
     * @return void
     */
    private function handleRuStoreFailed(NotificationFailed $event): void
    {
        /** @var RuStoreReport $report */
        $report = Arr::get($event->data, 'report');

        $report->all()->each(function (RuStoreSingleReport $singleReport, string $token) use ($report, $event): void {
            $e = $singleReport->error();
            Log::channel('notifications')->error('RuStoreFailed Ошибка отправки уведомления', [
                'user' => $event->notifiable->short_info,
                'token' => $token,
                'message' => $report->getMessage()->toArray(),
                'error_code' => $e->getCode(),
                'error_message' => $e->getMessage(),
            ]);
        });
    }

NOTE: Событие NotificationFailed поджигается только в случае наличия хотя-бы одной неуспешной отправки.

Available Message methods

Сообщение поддерживает все свойства, описанные в документации

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.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.