Download the PHP package tuyakhov/yii2-notifications without Composer
On this page you can find all versions of the php package tuyakhov/yii2-notifications. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tuyakhov/yii2-notifications
More information about tuyakhov/yii2-notifications
Files in tuyakhov/yii2-notifications
Package yii2-notifications
Short Description The extension provides support for sending notifications across a variety of delivery channels, including mail, SMS, Slack etc. Notifications may also be stored in a database so they may be displayed in your web interface.
License MIT
Homepage https://github.com/tuyakhov/yii2-notifications
Informations about the package yii2-notifications
:bell: Notifications for Yii2
This Yii2 extension provides support for sending notifications across a variety of delivery channels, including mail, SMS, Slack, Telegram etc. Notifications may also be stored in a database so they may be displayed in your web interface.
Typically, notifications should be short, informational messages that notify users of something that occurred in your application. For example, if you are writing a billing application, you might send an "Invoice Paid" notification to your users via the email and SMS channels.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require section of your composer.json
file.
Usage
The following example shows how to create a Notifier instance and send your first notification:
Notifier is often used as an application component and configured in the application configuration like the following:
Once the component is configured it may be used for sending notifications:
Each notification class should implement NotificationInterface
and contain a viaChannels
method and a variable number of message building methods (such as exportForMail
) that convert the notification to a message optimized for that particular channel.
Example of a notification that covers the case when an invoice has been paid:
You may use the NotifiableInterface and NotifiableTrait on any of your models:
Database notifications
The database
notification channel stores the notification information in a database table.
You can query the table to display the notifications in your application's user interface. But, before you can do that, you will need to create a database table to hold your notifications. To do this, you can use the migration that comes with this extension:
or
Accessing The Notifications
Once notifications are stored in the database, you need a convenient way to access them from your notifiable entities. The NotifiableTrait
, which comes with this extension, includes a notifications relationship that returns the notifications for the entity.
To fetch notifications, you may access this method like any other ActiveRecord
relationship.
If you want to retrieve only the "unread" notifications, you may use the unreadNotifications
relationship.
You can access custom JSON data that describes the notification and was added using DatabaseMessage
:
Marking Notifications As Read
Typically, you will want to mark a notification as "read" when a user views it. The ReadableBehavior
in Notification
model provides a markAsRead
method, which updates the read_at column on the notification's database record: