jeudi 22 novembre 2018

How to handle incoming notification in Laravel 5.6?

Good day,

I created a notification module which purpose is to notify the admin if there's an update in github repo.

What I've done so far:

  1. I installed guzzle
  2. Set a webhook in github for push event
  3. Implement some codes

Output: I received the data from my request...

Route::get('hook',function(){
            $client = new \GuzzleHttp\Client();
            $response = $client->request('GET', 'https://api.github.com/repos/user/learning-laravel');

            $data = json_decode($response->getBody()->getContents(),true);

            // dd($data);

            if(auth()->user()->hasRole('admin')){
                $this->notify(new \App\Notifications\WebhookNotification($data));
            }
            return view('pages.admin.system.webhook.index')->with(['data' => $data]);

        });

Route::get('/markAsRead',function(){
            auth()->user()->unreadNotifications->markAsRead();
            return redirect('iaccs-hook-list');
        })->name('markRead');

and here is my WebhookNotification.php

public function __construct()
    {
        //
    }

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

    /**
     * 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 toDatabase($notifiable)
    {
        return [
            //
        ];
    }

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

Problem: To be able to get notification, the route should be triggered first.

What I want to achieve: Real time notification, every time there's an update it will automatically notify the admin of the update.

Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire