• Stars
    star
    142
  • Rank 258,495 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 3 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Laravel Subscribable Notifications

Laravel Subscribable Notifications

Latest Version on Packagist Total Downloads GitHub Actions

This package allows you to subscribe your app Users to your app Notifications and dispatch them without specifying the recipient. The main goal of this package is allowing you to create "lists of recipients" for your Notification classes and dispatch notifications to your users easily.

Motivation

Recently I've been developing back-office kind of apps where a group of users need to be notified via email for a particular event or scenario in the system. These users were being hardcoded in the app and sometimes they needed to subscribe or be removed from certain emails, depending on the job they were doing at the company. Since their emails were hardcoded, it was tricky to update the "subscribers" list to each mailable that was sent by the application. With that idea in mind, this package was created to allow configuring users to each mailable they need to receive.

Installation

You can install the package via composer:

composer require asantibanez/laravel-subscribable-notifications

Afterwards, export both the config and migrations by using the php artisan vendor:publish command.

Once the laravel-subscribable-notifications.php config file is exported, make sure you define your User model and your subscribable_notifications (more on this below). Only the notifications you configure will be available for subscription.

Usage

Using Laravel notifications system, you normally dispatch notifications using the Notification facade or via the User model notify() method when implementing the Notifiable trait.

Notification::send([$user1, $user2, $user3], new OrderShipped($order));

// or 
$notification = new OrderShipped($order);

$user1->notify($notification);
$user2->notify($notification);
$user3->notify($notification);

With laravel-subscribable-notifications, you send the notification by "dispatching" it. Any subscribed user to the notification will receive it.

OrderShipped::dispatch($order);

Preparing your Notifications

Notifications must implement the SubscribableNotification interface which will require to implement the following methods:

  • dispatch
  • subscribers
  • subscribableNotificationType

For these, add the DispatchesToSubscriber trait to your notification which will cover dispatch and subscribers. Lastly, implement subscribableNotificationType by providing a string that identifies your notification. This value will be saved in the database for your users subscriptions.

class SalesOrderApprovedNotification extends Notification implements SubscribableNotification
{
    use DispatchesToSubscribers;

    public static function subscribableNotificationType(): string
    {
        return 'sales-order.approved';
    }
    
    // notification implementation here
}

You must also add your notification to the subscribable_notifications array inside laravel-subscribable-notifications config. Registering your notification here will allow the package to know which notifications can be dispatched throughout this interface.

Subscribing/Unsubscribing Users

Under the hood, a notification_subscriptions table is used to track all user subscriptions to your notifications.

Using the NotificationSubscriptionManager facade, you can subscribe and unsubscribe users from notification using the subscribe and unsubscribe methods respectively. There's also a unsubscribeFromAll method to remove all subscription from a user.

Note: Your User model can implement the HasNotificationSubscriptions trait to get helper methods in order to know what subscriptions each user has been subscribed to.

Utilities

The package register a new subscribable-notifications:manage command which you can run in your terminal and interact with the notification_subscriptions table. The command allows you to list/add/remove users from notifications you've defined in the laravel-subscribable-notifications config file.

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

More Repositories

1

livewire-calendar

Laravel Livewire component to show Events in a good looking monthly calendar
PHP
883
star
2

livewire-charts

Neat Livewire Charts for your Laravel projects
PHP
792
star
3

laravel-eloquent-state-machines

State Machines for your Laravel Eloquent models
PHP
523
star
4

livewire-select

Livewire component for dependant and/or searchable select inputs
PHP
499
star
5

laravel-blade-sortable

Custom Blade components to add sortable/drag-and-drop HTML elements in your apps.
PHP
408
star
6

livewire-status-board

Livewire component to show records according to their current status
PHP
338
star
7

livewire-resource-time-grid

Laravel Livewire component to show Events by time and resource in a good looking grid
PHP
220
star
8

Patio

A minimalistic Android view widget for selecting multiple images
Java
106
star
9

livewire-charts-demo

Livewire Charts demo app
PHP
69
star
10

Ranger

Android horizontally scrolled DatePicker
Java
57
star
11

livewire-dependant-select-demo

Laravel Livewire demo of multiple selects depending on each other values
PHP
48
star
12

Quota

Quota widget for Android
Java
31
star
13

livewire-calendar-demo

livewire-calendar component demo
PHP
23
star
14

laravel-eloquent-state-machines-demo

Demo repository for Laravel Eloquent State Machines package
PHP
17
star
15

OAuthWebView

WebViews for OAuth Authentication
Java
12
star
16

laravel-blade-sortable-demo

Demos for asantibanez/laravel-blade-sortable
PHP
11
star
17

livewire-wire-model-file-demo

Livewire lifecycle hook example for wire:model in file inputs
PHP
11
star
18

livewire-select-demo

Laravel app showcasing asantibanez/livewire-select component
PHP
10
star
19

practical

Practical ActiveRecord for DynamoDB
JavaScript
10
star
20

livewire-status-board-demo

Livewire Status Board demo app
PHP
7
star
21

Miveo

Vimeo SUPER AWESOME Android App
Java
4
star
22

laravel-inertia-infinite-scroll-demo

Demo for Infinite Scroll Feed in Laravel and InertiaJs
PHP
4
star
23

udacity-spotify-streamer-stage-2

Java
3
star
24

udacity-build-it-bigger

Udacity Build It Bigger Project
Java
2
star
25

udacity-go-ubiquitous

Project 6 of Udacity's Android Nanodegree program
Java
1
star
26

udacity-make-your-app-material

Project 5 of Udacity's Android Nanodegree program.
Java
1
star
27

vue-router-example

Vue.js project that uses Vue-Router
JavaScript
1
star
28

lienzo

Lienzo is a Horizontal/Vertical RecyclerView gallery powered by Picasso
Java
1
star
29

MarkdownViewer

Android Markdown View
Java
1
star
30

android-google-maps-test

Google Maps instructions for Android project
Java
1
star
31

laravel-paypal-server-checkout-test

PHP
1
star
32

livewire-charts-demo-livewire-v3

Livewire Charts Demo in Livewire v3
PHP
1
star
33

golden-layout-vue

Example of Golden Layout + Vue and Vuex integration
Vue
1
star
34

laravel-ecuadorian-taxpayer-validation-rule

Ecuadorian Taxpayer Validation Rule for Laravel
PHP
1
star
35

whatsapp-clone

WhatsApp Web UI clone using TailwindCss
Vue
1
star