1
votes

I am working with Laravel 5.3 Notifications.

I succeed to create database notifications and how to show all or unread those to target user.

For example this is my notification Class that notif to user her register Status in a Course :

class NewChangeRegisterStatusNotif extends Notification
{
    use Queueable;


    public function __construct ($status_id, $course_id)
    {
        $this->status = CourseUserStatus::findOrFail($status_id);
        $this->course = Course::findOrFail($course_id)->first(['title']);
    }


    public function via ($notifiable)
    {
        return ['database'];
    }


    public function toMail ($notifiable)
    {
        return (new MailMessage)
            ->line('The introduction to the notification.')
            ->action('Notification Action', 'https://laravel.com')
            ->line('Thank you for using our application!');
    }


    public function toArray ($notifiable)
    {
        return [
            'message' =>
                ' Your Status in ' .
                '<strong class="text-default">' . $this->course->title . '</strong>'
                . ' Course has changed to  ' .
                '<strong class="text-' . $this->status->label . '">' . $this->status->title . '</strong>';

            ,
            'action'  => '#'
        ];
    }
}

And to show all Notification I wrote in a blade template this :

<ul class="menu">
    @foreach($AuthUser->unreadNotifications as $notif)
        <li>
            <a href="{{$notif->data['action']}}" data-notif-id="{{$notif->id}}">
                {!! $notif->data['message'] !!}
            </a>
        </li>
    @endforeach
</ul>

All things work fine But Now I want to make that real-time.

I know that I can use Pusher and Pusher bridge. I install Pusher bridge and follow Building Real-Time Laravel Apps with Pusher guidance.

But I do not know how to use it in My notification? how can I define it in via() method?and what I do?

1

1 Answers

0
votes

For now you created the backend that will manage your notifications.

All you do in your view is displaying notifications from the server side rendering (that is, when laoding a new page).

If you really want realtime notification (and I assume you do because you mentionned pusher), you'll need to push notification from your server to the client side code of your application.

Following the tutorial you mentionned, the pushing server side is done by using this code:

$pusher->trigger( 'test-channel',
                  'test-event', 
                  array('text' => 'Preparing the Pusher Laracon.eu workshop!'));

Here your laravel app will push to pusher a message. All you have to do is to setup your client side (your javascript) to receive it and display it.

This part can be done using this sort of javascript code:

var pusher = new Pusher('{{env("PUSHER_KEY")}}');
var channel = pusher.subscribe('channel-name');
channel.bind('event-name', function(data) {
    // do something with the event data
});

It's up to you now to do whatever you need to display that notification. It can be as simple as a console.log to something really complicated like creating new html elements and adding them to your menu.

Sorry for not proposing actual code but I hope I made things clearer for you so you can carry on from that.