PHP code example of yakoffka / laravel-notification-channels-ru-store

1. Go to this page and download the library: Download yakoffka/laravel-notification-channels-ru-store 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/ */

    

yakoffka / laravel-notification-channels-ru-store example snippets


    /**
     * Получение массива ru-store пуш-токенов, полученных пользователем.
     * Используется пакетом laravel-notification-channels/rustore
     *
     * @return array
     */
    public function routeNotificationForRuStore(): array
    {
        return $this->ru_store_tokens;
    }


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',
                )
            )
        ));
    }
}


    // 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(),
            ]);
        });
    }


    // 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(),
            ]);
        });
    }

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