0

When in Laravel 9 app logged user fill ContactUs form I need to send email to site support and show notification in app for any logged support member. I make it with notification and pusher In app/Notifications/ContactUsCreatedNotification.php :

<?php

namespace App\Notifications;

use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use App\Models\User;

class ContactUsCreatedNotification extends Notification
{

    public $title;
    public $content_message;
    public $authorUser;

    public function __construct(string $title, string $content_message, User $authorUser)
    {
        $this->title           = $title;
        $this->content_message = $content_message;
        $this->authorUser      = $authorUser;
    }

    public function via($notifiable)
    {
        return ['mail', 'broadcast']; // I added broadcast here
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)->markdown('mail.ContactUsCreatedNotification', [
            'title'           => $this->title,
            'content_message' => $this->content_message,
            'authorUser'      => $this->authorUser
        ]);
    }

    public function toArray($notifiable)
    {
        return [
            'title'           => $this->title,
            'content_message' => $this->content_message,
            'authorUser'      => $this->authorUser,
        ];
    }
}

I got email on configured mailtrap but Debug console of my pusher app has no event : https://prnt.sc/L9nXEfup_3i-

in .env :

BROADCAST_DRIVER=pusher
CACHE_DRIVER=file
FILESYSTEM_DISK=local
SESSION_DRIVER=database
SESSION_LIFETIME=120

PUSHER_APP_ID=NNNN
PUSHER_APP_KEY=XXXXX
PUSHER_APP_SECRET=XXXXX

PUSHER_APP_CLUSTER=eu

In resources/js/bootstrap.js :

import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');

// alert('resources/js/bootstrap.js')
console.log('process.env.MIX_PUSHER_APP_KEY::')
console.log(process.env.MIX_PUSHER_APP_KEY)

console.log('process.env.MIX_PUSHER_APP_CLUSTER::')
console.log(process.env.MIX_PUSHER_APP_CLUSTER)


window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    forceTLS: true
});

In the app I have

"laravel/framework": "^v9.6.0",
"predis/predis": "^1.1",
"pusher/pusher-php-server": "^7.0",

and

"laravel-echo": "^1.11.7",

Any ideas why notification event is not triggered ?

UPDATED BLOCK # 1: in .env file I set parameters

QUEUE_CONNECTION=redis

I do :

    foreach ($supportManagers as $nextSupportManager) {
        if ($nextSupportManager->user) {
            $ret = Notification::sendNow($nextSupportManager->user, new ContactUsCreatedNotification(
                $request->title,
                $request->content_message,
                auth()->user()
            ));
        }
    }

Also I tried to add Queueable definition i app/Notifications/ContactUsCreatedNotification.php file:

<?php

namespace App\Notifications;

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

class ContactUsCreatedNotification extends Notification  implements ShouldQueue
{
    use Queueable;

    public $title;
    public $content_message;
    public $authorUser;

But I still get email in mailtrap and not events in pusher...

Thanks!

mstdmstd
  • 2,195
  • 17
  • 63
  • 140
  • can you past the full `ContactUsCreatedNotification.php` file ? – ml59 May 18 '22 at 08:22
  • I provided content of ContactUsCreatedNotification.php - just removed comments – mstdmstd May 18 '22 at 08:32
  • 2
    try to set the queue connection to sync `QUEUE_CONNECTION=sync` – ml59 May 18 '22 at 08:38
  • Thanks! It helped, but as I use redis in my app for queue of other emails. I my app some emails are queued in redis, but some of them must be run at once, just like ContactUsCreatedNotification. they conflict one with the other. Can it be salved ? – mstdmstd May 18 '22 at 10:14
  • 1
    maybe try to implement this interface `ShouldBroadcastNow` on your notification. Or using the `Notification::sendNow()` method https://laravel.com/docs/9.x/notifications#using-the-notification-facade – ml59 May 18 '22 at 10:19
  • Please look at UPDATED BLOCK # 1. Is it what you mean ? – mstdmstd May 18 '22 at 13:27
  • 1
    try to change your BROADCAST_DRIVER=log to check wether the problem is from pusher or your laravel. When your driver is log you should receive the notification in your laravel log – ml59 May 18 '22 at 13:42
  • I set BROADCAST_DRIVER=log anf after cache clearing I tryed again. No any notification in my laravel log. Something is wrong with settings of my site ? – mstdmstd May 18 '22 at 13:52
  • instead of `implements ShouldQueue` try `implements ShouldBroadcastNow` – ml59 May 18 '22 at 14:00
  • with ShouldBroadcastNow no events in Pusher Debug console – mstdmstd May 18 '22 at 14:09

0 Answers0