Laravel 8 Notifications - toSlack or toNexmo functions not firing

618 views Asked by At

I am having an issue with Slack and Nexmo.

Both are specified in the via function, but it seems that upon trigger, Laravel isn't hitting the toSlack or toNexmo functions, or the routeNotificationForSlack function. I have tried to exit or print something to prove it has gotten to this point but neither work.

I have also tried clearing cache (using DDEV as a local environment).

namespace App\Notifications;

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


class PaymentReceived extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

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

    /**
     * Get the Slack representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return SlackMessage
     */
    public function toSlack($notifiable)
    {
        exit;
        return (new SlackMessage)
            ->from('Ghost', ':ghost:')
            ->content('That was some good food.');
    }

    /**
     * Route notifications for the Slack channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForSlack($notification)
    {

        return 'MY_HOOK_HERE';
    }
}

Any help would be appreciated.

UPDATE

Thanks to @patricus below I needed to add this on my User Model instead. Thank you for your help :)

1

There are 1 answers

0
patricus On

The routeNotificationForSlack() method belongs on the notifiable object, not the notification.

If the notifiable object does not have this method defined, the slack notification channel will never actually call the toSlack() method.

Move your routeNotificationForSlack() method to the notifiable object and you should see your toSlack() method start to get called.