Giter Site home page Giter Site logo

notifier's Introduction

Notifier plugin for CakePHP

Travis Packagist Packagist Gitter

This plugin allows you to integrate a simple notification system into your application.

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install this plugin as composer package is:

    composer require bakkerij/notifier

Now load the plugin via the following command:

    bin/cake plugin load -b Bakkerij/Notifier

After loading the plugin you need to migrate the tables for the plugin using:

    bin/cake migrations migrate -p Bakkerij/Notifier

Sending notifications

Templates

Before sending any notification, we need to register a template. An example about how to add templates:

    $notificationManager->addTemplate('newBlog', [
        'title' => 'New blog by :username',
        'body' => ':username has posted a new blog named :name'
    ]);

When adding a new template, you have to add a title and a body. Both are able to contain variables like :username and :name. Later on we will tell more about these variables.

Notify

Now we will be able to send a new notification using our newBlog template.

    $notificationManager->notify([
        'users' => [1, 2],
        'recipientLists' => ['administrators'],
        'template' => 'newBlog',
        'vars' => [
            'username' => 'Bob Mulder',
            'name' => 'My great new blogpost'
        ]
    ]);

Note: You are also able to send notifications via the component: $this->Notifier->notify().

With the notify method we sent a new notification. A list of all attributes:

  • users - This is an integer or array filled with id's of users to notify. So, when you want to notify user 261 and 373, add [261, 373].
  • recipientLists - This is a string or array with lists of recipients. Further on you can find more about RecipientLists.
  • template - The template you added, for example newBlog.
  • vars - Variables to use. In the template newBlog we used the variables username and name. These variables can be defined here.

Recipient Lists

To send notifications to large groups you are able to use RecipientLists. You can register them with:

    $notificationManager->addRecipientList('administrators', [1,2,3,4]);

Now we have created a list of recipients called administrators.

This can be used later on when we send a new notification:

    $notificationManager->notify([
        'recipientLists' => ['administrators'],
    ]);

Now, the users 1, 2, 3 and 4 will receive a notification.

Retrieving notifications

Lists

You can easily retrieve notifications via the getNotifications method. Some examples:

    // getting a list of all notifications of the current logged in user
    $this->Notifier->getNotifications();

    // getting a list of all notifications of the user with id 2
    $this->Notifier->getNotifications(2);
    
    // getting a list of all unread notifications
    $this->Notifier->allNotificationList(2, true);

    // getting a list of all read notifications
    $this->Notifier->allNotificationList(2, false);

Counts

Getting counts of read/unread notifications can be done via the countNotifications method. Some examples:

    // getting a number of all notifications of the current logged in user
    $this->Notifier->countNotifications();

    // getting a number of all notifications of the user with id 2
    $this->Notifier->countNotifications(2);
    
    // getting a number of all unread notifications
    $this->Notifier->countNotificationList(2, true);

    // getting a number of all read notifications
    $this->Notifier->countNotificationList(2, false);

Mark as read

To mark notifications as read, you can use the markAsRead method. Some examples:

    // mark a single notification as read
    $this->Notifier->markAsRead(500;

    // mark all notifications of the given user as read
    $this->Notifier->markAsRead(null, 2);

Notification Entity

The following getters can be used at your notifications entity:

  • title - The generated title including the variables.
  • body - The generated body including the variables.
  • unread - Boolean if the notification is not read yet.
  • read - Boolean if the notification is read yet.

Example:

    // returns true or false
    $entity->get('unread');
    
    // returns the full output like 'Bob Mulder has posted a new blog named My Great New Post'
    $entity->get('body');

Passing to view

You can do something like this to use the notification list in your view:

    $this->set('notifications', $this->Notifier->getNotifications());

Notification Manager

The NotificationManager is the Manager of the plugin. You can get an instance with:

    NotificationManager::instance();

The NotificationManager has the following namespace: Bakkerij\Notifier\Utility\NotificationManager.

The manager has the following methods available:

  • notify
  • addRecipientList
  • getRecipientList
  • addTemplate
  • getTemplate

Notifier Component

The Bakkerij/Notifier.Notifier component can be used in Controllers:

    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('Bakkerij/Notifier.Notifier');
    }

The component has the following methods available:

  • getNotifications
  • countNotifications
  • markAsRead
  • notify

Keep in touch

If you need some help or got ideas for this plugin, feel free to chat at Gitter.

Pull Requests are always more than welcome!

notifier's People

Contributors

b1scuit avatar bobmulder avatar brandon1811 avatar dereuromark avatar hillelectric avatar jpkleemans avatar leoruhland avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

notifier's Issues

Add more info to readme

The readme is missing some important information, like an introductory sentence saying something like "This ... helps to ... " to explain to people what this plugin is for, and why they should use it.

NotificationsController could not be found.

when i install that composer package according the docs rule and then i run it on localhost as localhost/mann/notifications then it gives me error here could not found NotificationsController.
?which type of that issue.please help me

Add Adapters for modular extending through 3rd parties

Suggestion; implement kind of 'adapters' which are able took hook into notifications. Can be useful to connect to 3rd parties. This idea was brought internally and by #20.

Some ideas of adapters:

  • PusherAdapter
  • SocketIOAdapter
  • GoogleCMAdapter
  • FirebaseAdapter
  • WebhookAdapter
  • SlackAdapter
  • HipchatAdapter

(the idea of adapters can be seen like http://flysystem.thephpleague.com/)

If you want to join the realization of this implementation, feel free to hang into this issue!

Some points of discussion:

  • For sure we need to make use of a base adapter to create new adapters on
  • A cake bake command for adapters would be cute :)
  • Technically, the base adapter should listen to several events
  • What would be a standard Adapter class?

Problem with CakePHP 3.1

Hi !
Thank you for your plugin.

I would like use this plugin on CakePHP 3.1.x and i have this error when trying to get the number of notification :
Error: Call to undefined method Notifier\Utility\NotificationManager::countsNotifications()

I think the NotifierComponent don't load with the plugin.

Thank you for your answer.

(i'm using CakeDC/Users for User system and user_id is not in INT but in CHAR(36))

How to render notification to view ?

Hello, I am new in CakePHP.

Here is my code -

// bootstrap.php
  `use Notifier\Utility\NotificationManager;
Plugin::load('Migrations');
Plugin::load('Notifier', ['bootstrap' => true, 'routes' => true, 'autoload' => true]);
$notificationManager = new NotificationManager();
$notificationManager->addTemplate('add', [
    'title' => 'New blog by :username',
    'body' => ':username has posted a new blog named :name'
]);`

// BookmarksController.php
`public function index()
    {
        $this->paginate = [
            'contain' => ['Users', 'Tags']
        ];
        $bookmarks = $this->paginate($this->Bookmarks);
        $this->Notifier->notify([
            'users' => [1],
            'template' => 'add',
            'vars' => [
                'username' => 'Aditya',
                'name' => 'My great new blogpost'
            ],
        ]);
public function add() {....}`

I am not seeing any notification.
I don't understand how to use this plugin?
Please describe in details.
Thank You

Improve templating

Hi! Thanks for the amazing work.

I have suggestion. Would it be better to handle templates in config files instead?
It would allow an easier integration process.

A config file would allow a few more improvements for he plugin such as
instead of doing this:

`$this->Notifier->allNotificationList(2, true);``

We could use config to set env var (or similar) to do this:

$this->Notifier->allNotificationList(2, __UNREAD__);

What do you think? Tell me if you need a PR on this one.

Should we consider this repo abandonned?

As I can see the last commit f913660 was 6 years ago.

If the plugin is no longer being maintained, could you suggest any alternatives or forks? Additionally, if there's a way for the community to assist or take over maintenance, please let us know.

In CakeMojo we are doing some updates (I have had to update it for a Open Source project). I guess that everyone is working in this plugin by yourself in your own fork and you have some ideas to improve this plugin, please don't hesitate to suggest them and collaborate.

Install failes

Can not install the plugin:
php composer.phar create-project --prefer-dist cakephp/app cakeadmintestapp
cd cakeadmintestapp
php ../composer.phar require cakeplugins/notifier
bin/cake plugin install -b Notifier

fails: Error: Unknown short option b

If I use the load plugin it works:
bin/cake plugin load Notifier

But the next step fails:
bin/cake migrations migrate Notifier
Exception: Plugin Notifier could not be found. in [/var/www/vhosts/workspace_us/htdocs/cakeadmintestapp/vendor/cakephp/cakephp/src/Core/Plugin.php, line 148]

Create a behavior to send out auto notifications

As you know keeping controllers clean and implementing the data related logic into model level is a better approach for CakePHP in a few different ways.

So, since the notifications are created with templates, I often find myself using variables from my entity just after saving it. For example; I want to notify all users when I add a new record to my posts. So I suggest thinking about something like below:

    $this->addBehavior('bakkerij/notifier.notifier', [
                'template' => 'someTemplateName',
                'keywords' => [
                    'keyword1' => 'columnNameOne',
                    'keyword2' => 'columnNameTwo',
                ],
                'to' => 'recipentListName?'
    ]);

Not working on CakePHP 3.4.3 ?

Hello guys,

I'm trying to install the plugin, but i receive the following message after the command "composer require bakkerij/notifier"

Error message:
error

Any ideas ?

Thanks in advance!

Pedro

allNotificationList() is not working

Here is my code -
dump($this->Notifier->allNotificationList($userid, true));

got Error :

Error: Call to undefined method Notifier\Controller\Component\NotifierComponent::allNotificationList()
File /Users/adityagupta/talkadoc/vendor/cakedc/users/src/Controller/UsersController.php
Line: 70

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.