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