Notifier plugin for CakePHP
, (*1)
This plugin allows you to integrate a simple notification system into your application., (*2)
Installation
You can install this plugin into your CakePHP application using composer., (*3)
The recommended way to install this plugin as composer package is:, (*4)
composer require bakkerij/notifier
Now load the plugin via the following command:, (*5)
bin/cake plugin load -b Bakkerij/Notifier
After loading the plugin you need to migrate the tables for the plugin using:, (*6)
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:, (*7)
$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., (*8)
Notify
Now we will be able to send a new notification using our newBlog
template., (*9)
$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()
., (*10)
With the notify
method we sent a new notification. A list of all attributes:, (*11)
-
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:, (*12)
$notificationManager->addRecipientList('administrators', [1,2,3,4]);
Now we have created a list of recipients called administrators
., (*13)
This can be used later on when we send a new notification:, (*14)
$notificationManager->notify([
'recipientLists' => ['administrators'],
]);
Now, the users 1, 2, 3 and 4 will receive a notification., (*15)
Retrieving notifications
Lists
You can easily retrieve notifications via the getNotifications
method. Some examples:, (*16)
// 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:, (*17)
// 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:, (*18)
// 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., (*19)
Example:, (*20)
// 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:, (*21)
$this->set('notifications', $this->Notifier->getNotifications());
Notification Manager
The NotificationManager
is the Manager of the plugin. You can get an instance with:, (*22)
NotificationManager::instance();
The NotificationManager
has the following namespace: Bakkerij\Notifier\Utility\NotificationManager
., (*23)
The manager has the following methods available:, (*24)
notify
addRecipientList
getRecipientList
addTemplate
getTemplate
Notifier Component
The Bakkerij/Notifier.Notifier
component can be used in Controllers:, (*25)
public function initialize()
{
parent::initialize();
$this->loadComponent('Bakkerij/Notifier.Notifier');
}
The component has the following methods available:, (*26)
getNotifications
countNotifications
markAsRead
notify
Keep in touch
If you need some help or got ideas for this plugin, feel free to chat at Gitter., (*27)
Pull Requests are always more than welcome!, (*28)