mrad/notifications-bundle

Implement notifications system in your symfony project using Pusher API

Installs: 500

Dependents: 0

Suggesters: 0

Stars: 6

Watchers: 2

Forks: 3

Open Issues: 0

Type:symfony-bundle

v1.3.0 2019-01-23 09:13 UTC

README

A simple implementation for Pusher API that helps you integrate a notifications system in a few simple steps.

Installation

  1. composer require mrad/notifications-bundle
  2. Enable the bundle in AppKernel.php new SBC\NotificationsBundle\NotificationsBundle(),

Usage

Step 1:

First thing you need to create an account with Pusher and then in Your apps menu create a new app.

Step 2:

Add you app configuration to the app/config.yml:

# NotificationsBundle configuration
notifications:
    app_id:     your_id
    app_key:    your_key
    app_secret: secret
    cluster:    cluster
    # Optional
    # Default false: work without ssl encryption
    # Set it to true to work with ssl encryption
    encrypted:  false

Of course you can find those details in your app in the dashboard.
alt text

Step 3:

Now in your view.html.twig (your client side) add this:

{# Call NotificationsBundles's assets #}
{{ notifications_assets() }}
<script>
    /**
    * After calling notifications_assets() "pusher" is now available
    * and you can use it this way
    */

    // select the channel you want to listen to
    var channel = pusher.subscribe("notifications");// notifications channel
    channel.bind("my-event", function(data) {
        console.log('from notifications channel', data);
    });

    var channel = pusher.subscribe("messages");// messages channel
    channel.bind("my-event", function(data) {
        console.log('from messages channel', data);
    });
</script>

And that's it 😃, now to make sure that your client is receiving the data correctly you can test it by calling this console command:
php bin/console notifications:trigger "Your message" "channel"
If you open the browser's console you should see something like this:
alt text

Broadcast custom messages from Backend

To broadcast messages from your backend you can simply do this:

// From your controller or service
$data = array(
    'my-message' => "My custom message",
);
$pusher = $this->get('mrad.pusher.notificaitons');
$channel = 'messages';
$pusher->trigger($data, $channel);

// or you can keep the channel pram empty and will be broadcasted on "notifications" channel by default
$pusher->trigger($data);

Next Step

Now I will show how to create and save notifications in database.

License

This project is under the MIT license

Videos

You can check this playlist for more details about how This bundle works.

Thanks

Thanks to SlimenTN for his help in this project