Download the PHP package dvamigos/yii2-notifications without Composer
On this page you can find all versions of the php package dvamigos/yii2-notifications. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dvamigos/yii2-notifications
More information about dvamigos/yii2-notifications
Files in dvamigos/yii2-notifications
Package yii2-notifications
Short Description Notification system for Yii 2+
License MIT
Informations about the package yii2-notifications
yii2-notifications
Notification system for Yii 2+
Usage
Default storage is your database. For that you need to insert migrations from src/migrations
folder.
To insert those migrations you need to run migration command:
This will create notification
table in your database. If you don't plan to store your notifications in database then this step is not necessary.
Add notification component configuration in your application config:
Then in your code you can push notifications directly using:
This will save the notification for current logged in user.
Notification types
You can define arbitrary number of types for your notification to store as much as data per notification as needed.
Below is an example of a notification having a title
and a message
.
Field {username}
will be replaced from data passed to notification on its creation.
To pass data just use:
You do not have to always pass every necessary key into data. If you do not pass required key it's value will be taken from 'default'
of that
notification.
You can also use this array to pass any arbitrary information which can be serialized into JSON string, effectively allowing you to store any required data in order to display or use your notification.
Using in models/controllers/components
You can use PushNotification
, UpdateNotification
or ReplaceNotification
classes inside your every component which has events()
function.
To include events()
function use EventListAwareTrait
.
For example to set it inside a model you can define following:
Types can be resolved later using:
If you wish to use Behavior approach, that is also available via NotificationBehavior
class.
To use that class you can simply add in your model/component which supports behaviors:
If you want to encapsulate your own logic, then extending \dvamigos\Yii2\Notifications\events\PushNotification
with your own class is also a possibility.
Example:
Then you can add your notification simply as:
Multiple targets
You can define multiple targets when sending notifications effectively allowing you push notifications on phone devices, database, etc at the same time.
Your default target is database but you can define your own using:
Note
Please not that Android and iOS targets are WIP and not completely tested and might not work properly.
Then using above configuration, when calling:
You will send notification to database, your user's android device and ios device.
You can switch targets at any time using:
Please note that you should use popTarget()
to restore old behavior. Example:
You can call specific target using:
Or you can execute command on specific targets using forTargets()
Token retrieval
Since some targets require additional token data for sending notification you can implement your own token retrieval logic based on which target needs which token.
Token retriever can be a callable
closure:
tokenRetriever
callable will be called every time token is required for sending.
tokenRetriever
can also be a class which implements dvamigos\Yii2\Notifications\TokenRetrievalInterface
.
Class:
Configuration:
Displaying notifications
You can use NotificationList
widget to display your notifications.
Please note that depending on your use case you will need to configure this widget to suit your needs. This widget assumes that
your component name is notification
although you can pass a different name during widget creation.
You need to specify template for your notifications using this widget.
Example of a simple list where notifications are defined as.
Custom sections are also supported. And you can define them as:
Please refer to NotificationList
documentation in the code for more information on what is available.
Best practice is to extend this NotificationList
widget with your own and implement this functionality
based on needs for your own project.