mercredi 20 juin 2018

Custom Notification Channel Not Used When Sending Notification

Running: PHP 7.1.3/Laravel 5.6.*

I've successfully been able to use the notification channels that come out of the box with Laravel, but for some reason custom notification channels are not being used let alone constructed.

Expected:

Invoking $user->notify(new ContactResponse()) should result in PushChannel's send method being invoked.

Actual:

When I invoke $user->notify(new ContactResponse()), the channel specified in ContactResponse's via method (PushChannel) never invokes send, let alone its constructor.

What I've done:

I've added logging statements to all relevant method bodies to verify the issue and know that:

  • ContactResponse's via method is invoked, but its toPush is not.
  • PushChannel's constructor and send methods are never invoked.
  • Adding 'mail' to the channels in ContactResponse's via method & a toMail method to the class does get invoked on that channel as expected.

I've double checked the Laravel 5.6 docs relating to this and found no disparity between the example implementation and mine.

I've reviewed the issues/PRs for the Laravel framework and found this suggesting that it's an issue within Laravel's current implementation of custom notification channels:

https://github.com/laravel/framework/pull/24223

Question:

I know that Laravel has extensions for push notification channels but we would like to use our custom implementation. Is there a workaround we can leverage so that PushChannel gets used without having to modify Laravel's source code?


ContactResponse.php

<?php

namespace App\Notifications;

// ...imports

class ContactResponse extends Notification implements ShouldQueue 
{
    use Queueable;

    public function via(User $notifiable)
    {
        return [PushChannel::class];
    }

    public function toPush(User $notifiable)
    {
        return [/*payload*/]
    }
}

PushChannel.php

<?php

namespace App\Channels;

// ...imports

class PushChannel
{
    public function send(User $notifiable, Notification $notification)
    {
        $payload = $notification->toPush($notifiable)
        // business logic
    }
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2K1ZUHp
via IFTTT

Aucun commentaire:

Enregistrer un commentaire