2

In an event listener, I send a notification to a dog owner like this:

$event->dogowner->notify(new DogWasWalkedNotification);

The issue is since there are two channels database/mail set in the Notification below, they both get added as queued jobs on the 'notifications' queue, set in the constructor. Instead, I want to add the mail channel below to an emails queue instead of the default one set in the constructor of 'notifications'.

Any idea how to have the following notification only add the mail channel MailMessage to an emails queue without adding the database channel to a queue at all? (even if that means removing the constructor onQueue)

<?php
namespace App\Notifications;

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

class DogWasWalkedNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $event;

    public function __construct(DogWasWalked $event) {
        $this->event = $event;

        // This is what creates 2 queued jobs (one per channel)
        $this->onQueue('notifications');
    }

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

    public function toArray($notifiable) {
        return [
            'activity' => 'Dog was walked!',
            'walkername' => $this->event->walkername
        ];
    }

    public function toMail($notifiable) {
      // How to set the queue name for
      // this channel to 'emails'
      return (new MailMessage)
             ->line('Dog was walked!');

    }
}
Wonka
  • 8,244
  • 21
  • 73
  • 121
  • `database` and `mail` are channels this notification should send to, and they respects to the queue you specified so I don't think it can be done. Did you mean you want to add this notification to database without queue, but queue the mails? I suggest split the notification up if that's the case. – Lionel Chan Mar 20 '18 at 02:35
  • Yup, that's exactly what I meant. What do you mean by split them up? – Wonka Mar 20 '18 at 02:43
  • 2
    One notification for mail, another for database, that's what I can think of, since there is no way to split them up via one notification – Lionel Chan Mar 20 '18 at 04:20
  • 1
    Yeah, but I was really trying to keep all channels in notification with different queues as needed. So db no queue, but toMail `emails` queue, and as another example of what I am trying to achieve toSlack 'slack' queue. I have a feeling there must be a way to assign different channels to different queues under one notification... – Wonka Mar 20 '18 at 04:26
  • Hmm, if that's the case, you might want to extend NotificationSender and create one for your own usage. [Check the core codes here](https://github.com/laravel/framework/blob/f4eb7056751dd27ab7e8d57a4dd0473315b8e357/src/Illuminate/Notifications/NotificationSender.php#L151-L162), it uses just one queue for all channels, which make sense for almost all common cases. – Lionel Chan Mar 20 '18 at 04:42

0 Answers0