Skip to content

Instantly share code, notes, and snippets.

@rishpandey
Created April 19, 2021 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rishpandey/2689c481cc9e3209223cbf2e47d17449 to your computer and use it in GitHub Desktop.
Save rishpandey/2689c481cc9e3209223cbf2e47d17449 to your computer and use it in GitHub Desktop.
PreferenceBasedNotification
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
abstract class PreferenceBasedNotification extends Notification
{
use Queueable;
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
final public function via($notifiable)
{
$viaChannels = [];
$preference = $this->getPreference($notifiable->user_id);
if (!$preference || $preference->receive_email) {
$viaChannels[] = 'mail';
}
if (!$preference || $preference->receive_notification) {
$viaChannels[] = 'broadcast';
}
return $viaChannels;
}
final private function getPreference($userId)
{
$currentClass = \Arr::last(explode('\\', get_class($this)));
$event = \DB::table('notification_events')
->where('notification_class', $currentClass)->first();
if (!$event) {
return null;
}
return DB::table('user_notification_preferences')
->where('user_id', $userId)
->where('notification_event_id', $event->id)
->first();
}
abstract public function toMail($notifiable);
abstract public function toBroadcast($notifiable);
abstract public function getDependentNetworkID();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment