Paradox
12 ماه پیش توسط Paradox مطرح شد
0 پاسخ

مشکل در ارسال نوتیفیکیشن

سلام من میخوام وقتی خبر جدید تو سایت گذاشته شد از طریق laravel broadcast notification برای تمام کاربران سایت یک نوتیفیکشن ارسال بشه که خبر جدید در سایت دارید
به روش زیر انجام دادم اما وقتی خبر جدید تو دیتابیس ثبت میکنم به هیچ عنوان نوتیفیکشن ارسال نمیشه
laravel echo , pusher نصب کردم ولی هنوز کار نمیکنه تو کنسول هم خطایی ندارم لطفا راهنمایی کنید مشکل کارم کجاست؟

Broadcast::channel('news', function ($user) {
    return true;
});

event 

class NewsAdded implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $news;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($news)
    {
        $this->news = $news;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('news');
    }

    public function broadcastAs()
    {
        return 'news.added';
    }

    public function broadcastWith()
    {
        return [
            'news' => $this->news,
        ];
    }

    public function broadcastAfterCommit()
    {
        NewsNotification::dispatch($this->news);
    }
}

notification

class NewsNotification extends Notification
{
    use Queueable;

    public $news;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($news)
    {
        $this->news = $news;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['broadcast'];
    }

    public function toBroadcast($notifiable)
    {
        return new BroadcastMessage([
            'data' => [
                'news' => $this->news,
            ],
            'notification' => [
                'title' => 'خبر جدید دارید',
                'body' => 'A new news has been added!',
                'click_action' => route('news', $this->news->slug),
            ],
        ]);
    }
    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

comtroller 
    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'slug' => 'unique:news',
            'status' => '',
            'description' => 'required',
        ]);

        if (empty($request->slug)) {
            $slug = SlugService::createSlug(News::class, 'slug', $request->name);
        } else {
            $slug = SlugService::createSlug(News::class, 'slug', $request->slug);
        }
        $request->merge(['slug' => $slug]);

        $news = auth()->user()->news()->create($request->all());

        event(new NewsAdded($news));

        alert()->success('خبر مورد نظر با موفقیت ثبت شد' , 'با تشکر');
        return redirect(route('news.index'));
    }

    blade

        @if (session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
        @endif

    <div id="notification-container"></div>

bootstrap.js

window.Echo.private('news')
    .notification(notification => {
        let container = document.getElementById('notification-container');
        let notificationElement = document.createElement('div');
        notificationElement.classList.add('alert', 'alert-primary', 'alert-dismissible', 'fade', 'show');
        notificationElement.setAttribute('role', 'alert');
        notificationElement.innerHTML = `
            <strong>${notification.title}</strong> ${notification.body} 
            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        `;
        container.appendChild(notificationElement);
    }); 

ثبت پرسش جدید

به همدیگه کمک کنیم

به Paradox کمک کنید تا مشکل خودش را حل کند؛ این‌طور می‌توانیم با هم پیشرفت کنیم.

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام