Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to unsubscribe from a notification that implements the getSubscriptionModel() function #6

Open
michaelhume opened this issue Jan 22, 2021 · 1 comment

Comments

@michaelhume
Copy link

Hello,

Thanks for the package - this is the exact functionality I was looking to implement.

I have created a base notification class that I plan to use for all notifications on a given Class. The base notification implements an interface which declares getSubscriptionModel().

The goal is to be able to unsubscribe from notifications for any particular Model of that class; however, if it's setup like this, it breaks attempts to unsubscribe from the Notification class itself.

ex:

interface CanUnsubscribe
{
    public function getSubscriptionModel($notifiable) : \Illuminate\Database\Eloquent\Model;
}
abstract class RequestNotification extends Notification implements ShouldQueue, CanUnsubscribe
{
    use Queueable;

    public Request $request;

    public bool $ignoreSubscriptions;

    public function __construct(Request $request, $ignore = false)
    {
        $this->request = $request;
        $this->ignoreSubscriptions = $ignore;
    }

    public function getSubscriptionModel($notifiable): \Illuminate\Database\Eloquent\Model
    {
        return $this->request;
    }
}
class NotifyRequestStateChanged extends RequestNotification
{
  //  ... concrete notification ....
}

Then if you try

$user->unsubscribe(NotifyRequestStateChanged)

The user will still receive the notification unless you specify the model in the unsubscribe call.

I would expect that if you've unsubscribed from a notification, you should not receive that notification, regardless of it's implementation of the getSubscriptionModel() function or not.

@urbanfivable
Copy link

urbanfivable commented Oct 24, 2024

Hi @michaelhume ! I encountered a similar issue in my app and discovered that you can bypass this behavior by checking to see if the $notifiable passed into the getSubscriptionModel() method has a model-specific NotificationSubscription for the Notification.

For example:

public function getSubscriptionModel($notifiable)
{
    return $notifiable->notificationSubscriptions()->model($this->yourModel)->doesntExist()
        ? null
        : $this->yourModel;
}

Whenever getSubscriptionModel() returns null, the subscription logic treats it as though there should be no subscription-model requirement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants