Thursday, March 9, 2017

Laravel pretty outputting notifications

I'm building an website using Laravel 5.4 and I'm now stuck with Laravel's Notifications System. This is how my Notification class looks like

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class RideBooked extends Notification
{
    use Queueable;


    public $booking;

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

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

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('New ride created.')
                    ->action('Notification Action', url('/'));
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'booking_id' => $this->booking->id,
            'user_id'    => $this->booking->user_id,
        ];
    }    
}

`

I'm sending notification when a Booking is made:

$user->notify( new RideBooked( $booking ) );

This is how notifications are stored in the database

So, I'm having two questions: 1. How do I output these notifications in a pretty way ? I need to process Id's stored in notification->data Array., to retrieve Booking from data['booking_id'], and maybe pass it to a view, so where should I perform this processing ? 2. I want to watch in real-time for new notifications, for this purpose, I'm going to use Pusher , I've done some basic configuration for Pusher, but I can't figure out what's next. I've also created an RideBooked event

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class RideBooked extends Notification
{
    use Queueable;


    public $booking;

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

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

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('New ride created.')
                    ->action('Notification Action', url('/'));
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'booking_id' => $this->booking->id,
            'user_id'    => $this->booking->user_id,
        ];
    }    
}

And I'm triggerind it whenever a Ride is Booked. But how do I listen to this event in Pusher ? If you could help me, that would be great



via Nicolae Cojocaru

Advertisement