All Projects → tuyakhov → yii2-notifications

tuyakhov / yii2-notifications

Licence: other
This Yii2 extension provides support for sending notifications across a variety of delivery channels, including mail, SMS, Slack, Telegram etc.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to yii2-notifications

Yii2 Bootstrap
Yii 2 Bootstrap 3 Extension
Stars: ✭ 177 (+185.48%)
Mutual labels:  yii2, yii
Yii2 Bootstrap4
Yii 2 Bootstrap 4 Extension
Stars: ✭ 204 (+229.03%)
Mutual labels:  yii2, yii
Yii2 Debug
Debug Extension for Yii 2
Stars: ✭ 179 (+188.71%)
Mutual labels:  yii2, yii
Rageframe
基于yii2的应用开发引擎
Stars: ✭ 170 (+174.19%)
Mutual labels:  yii2, yii
install
basic script for project installation
Stars: ✭ 17 (-72.58%)
Mutual labels:  yii2, yii
Crontab
Yii2 extension for crontab support
Stars: ✭ 170 (+174.19%)
Mutual labels:  yii2, yii
Ar Softdelete
Soft delete behavior for ActiveRecord
Stars: ✭ 188 (+203.23%)
Mutual labels:  yii2, yii
Yii2 Swagger
yii2 with swagger-php
Stars: ✭ 138 (+122.58%)
Mutual labels:  yii2, yii
Yii2 Apidoc
Yii 2 apidoc extension.
Stars: ✭ 236 (+280.65%)
Mutual labels:  yii2, yii
Iisns
sns 开放社区
Stars: ✭ 217 (+250%)
Mutual labels:  yii2, yii
Balance
Balance accounting (bookkeeping) system based on debit and credit principle
Stars: ✭ 162 (+161.29%)
Mutual labels:  yii2, yii
yii2-grid-view-library
Highly enhanced GridView widget and grid components for Yii2
Stars: ✭ 57 (-8.06%)
Mutual labels:  yii2, yii
Yii2 Cms
YiiCMS - 基于 Yii2 的高度可定制化开源 CMS
Stars: ✭ 153 (+146.77%)
Mutual labels:  yii2, yii
Yii2 Sphinx
Yii 2 Sphinx extension.
Stars: ✭ 172 (+177.42%)
Mutual labels:  yii2, yii
Yii2 Assets Auto Compress
Automatic compilation of js + css + html
Stars: ✭ 147 (+137.1%)
Mutual labels:  yii2, yii
Yii2 Gii
Yii 2 Gii Extension
Stars: ✭ 183 (+195.16%)
Mutual labels:  yii2, yii
Yii2 Shell
Interactive shell
Stars: ✭ 129 (+108.06%)
Mutual labels:  yii2, yii
Yii2 Twig
Yii 2 Twig extension.
Stars: ✭ 130 (+109.68%)
Mutual labels:  yii2, yii
Yii2
Yii 2: The Fast, Secure and Professional PHP Framework
Stars: ✭ 13,852 (+22241.94%)
Mutual labels:  yii2, yii
yii2-admin-theme
基于Yii2+layui的后台框架模板,实现了完善的RBAC权限控制
Stars: ✭ 87 (+40.32%)
Mutual labels:  yii2, yii

🔔 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.

Latest Stable Version Scrutinizer Code Quality Build Status Code Climate

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist tuyakhov/yii2-notifications "*"

or add

"tuyakhov/yii2-notifications": "*"

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 = new \tuyakhov\notifications\Notifier([
  'channels' => [...],
]);
$notifier->send($recipients, $notifications);

Notifier is often used as an application component and configured in the application configuration like the following:

[
   'components' => [
       'notifier' => [
           'class' => '\tuyakhov\notifications\Notifier',
           'channels' => [
               'mail' => [
                   'class' => '\tuyakhov\notifications\channels\MailChannel',
                   'from' => '[email protected]'
               ],
               'sms' => [
                   'class' => '\tuyakhov\notifications\channels\TwilioChannel',
                   'accountSid' => '...',
                   'authToken' => '...',
                   'from' => '+1234567890'
               ],
               'telegram' => [
                    'class' => '\tuyakhov\notifications\channels\TelegramChannel',
                    'botToken' => '...'
                ],
               'database' => [
                    'class' => '\tuyakhov\notifications\channels\ActiveRecordChannel'
               ]
           ],
       ],
   ],
]

Once the component is configured it may be used for sending notifications:

$recipient = User::findOne(1);
$notification = new InvoicePaid($invoice);

Yii::$app->notifier->send($recipient, $notification);

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:

use tuyakhov\notifications\NotificationInterface;
use tuyakhov\notifications\NotificationTrait;

class InvoicePaid implements NotificationInterface
 {
    use NotificationTrait;
    
    private $invoice;
    
    public function __construct($invoice) 
    {
        $this->invoice = $invoice;
    }

    /**
     * Prepares notification for 'mail' channel
     */
    public function exportForMail() {
        return Yii::createObject([
           'class' => '\tuyakhov\notifications\messages\MailMessage',
           'view' => ['html' => 'invoice-paid'],
           'viewData' => [
               'invoiceNumber' => $this->invoice->id,
               'amount' => $this->invoice->amount
           ]
        ])
    }
    
    /**
     * Prepares notification for 'sms' channel
     */
    public function exportForSms()
    {
        return \Yii::createObject([
            'class' => '\tuyakhov\notifications\messages\SmsMessage',
            'text' => "Your invoice #{$this->invoice->id} has been paid"
        ]);
    }
    
    /**
     * Prepares notification for 'database' channel
     */
    public function exportForDatabase()
    {
        return \Yii::createObject([
            'class' => '\tuyakhov\notifications\messages\DatabaseMessage',
            'subject' => "Invoice has been paid",
            'body' => "Your invoice #{$this->invoice->id} has been paid",
            'data' => [
                'actionUrl' => ['href' => '/invoice/123/view', 'label' => 'View Details']
            ]
        ]);
    }
 }

You may use the NotifiableInterface and NotifiableTrait on any of your models:

use yii\db\ActiveRecord;
use tuyakhov\notifications\NotifiableTrait;
use tuyakhov\notifications\NotifiableInterface;

class User extends ActiveRecord implements NotifiableInterface 
{
   use NotifiableTrait;
   
   public function routeNotificationForMail() 
   {
        return $this->email;
   }
}

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:

yii migrate --migrationPath=@vendor/tuyakhov/yii2-notifications/src/migrations

or

'controllerMap' => [
    ...
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        'migrationNamespaces' => [
            'tuyakhov\notifications\migrations',
        ],
    ],
    ...
],
php yii migrate/up

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.

$model = User::findOne(1);
foreach($model->notifications as $notification) {
    echo $notification->subject;
}

If you want to retrieve only the "unread" notifications, you may use the unreadNotifications relationship.

$model = User::findOne(1);
foreach($model->unreadNotifications as $notification) {
    echo $notification->subject;
}

You can access custom JSON data that describes the notification and was added using DatabaseMessage:

/** @var $notificatiion tuyakhov\notifications\models\Notificatios */
$actionUrl = $notification->data('actionUrl'); // ['href' => '/invoice/123/pay', 'label' => 'Pay Invoice']

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:

$model = User::findOne(1);
foreach($model->unreadNotifications as $notification) {
    $notification->markAsRead();
    
    // the following methods are also available
    $notification->markAsUnread();
    $notification->isUnread();
    $notification->isRead();
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].