PHPackages                             overplex/yii2-notifications - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. overplex/yii2-notifications

ActiveYii2-extension[Mail &amp; Notifications](/categories/mail)

overplex/yii2-notifications
===========================

Notifications module for Yii2

0.2(8y ago)0120PHPPHP &gt;=5.6

Since Oct 11Pushed 5y agoCompare

[ Source](https://github.com/overplex/yii2-notifications)[ Packagist](https://packagist.org/packages/overplex/yii2-notifications)[ RSS](/packages/overplex-yii2-notifications/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

yii2-notifications
==================

[](#yii2-notifications)

This module provides a way to sending notifications across a variety of delivery channels, including mail, screen, SMS (via Nexmo), etc. Notifications may also be stored in a database so they may be displayed in your web interface.

Notifications are short 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.

 [![Yii2 Notifications Module](https://camo.githubusercontent.com/db88eb2ba6b662ff405346bfa7a99a39328d5e26e623df3d176e7962000c5d49/687474703a2f2f352e3138392e3135302e3134352f32313632313934375f31303135353639353031313035383337375f3635393333343639332e6a7067)](https://camo.githubusercontent.com/db88eb2ba6b662ff405346bfa7a99a39328d5e26e623df3d176e7962000c5d49/687474703a2f2f352e3138392e3135302e3134352f32313632313934375f31303135353639353031313035383337375f3635393333343639332e6a7067)

Requirements
------------

[](#requirements)

- PHP 7.1+

    - gmp
    - mbstring
    - curl
    - openssl
- PHP 7.2+ is recommended for better performance.

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

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

```

or add

```
"webzop/yii2-notifications": "*"
```

to the require section of your `composer.json` file.

Usage
-----

[](#usage)

Notifications is often used as an application module and configured in the application configuration like the following:

```
[
    'modules' => [
        'notifications' => [
            'class' => 'webzop\notifications\Module',
            'channels' => [
                'screen' => [
                    'class' => 'webzop\notifications\channels\ScreenChannel',
                ],
                'email' => [
                    'class' => 'webzop\notifications\channels\EmailChannel',
                    'message' => [
                        'from' => 'example@email.com'
                    ],
                ],
                'web' => [
                    'class' => 'webzop\notifications\channels\WebChannel',
                    'enable' => true,                                       // OPTIONAL (default: true) enable/disable web channel
                    'config' => [
                        'serviceWorkerFilepath' => '/service-worker.js',    // OPTIONAL (default: /service-worker.js) is the service worker filename
                        'serviceWorkerScope' => '/app',                     // OPTIONAL (default: './' the service worker path) the scope of the service worker: https://developers.google.com/web/ilt/pwa/introduction-to-service-worker#registration_and_scope
                        'serviceWorkerUrl' => 'url-to-serviceworker',       // OPTIONAL (default: Url::to(['/notifications/web-push-notification/service-worker']))
                        'subscribeUrl' => 'url-to-subscribe-handler',       // OPTIONAL (default: Url::to(['/notifications/web-push-notification/subscribe']))
                        'unsubscribeUrl' => 'url-to-unsubscribe-handler',   // OPTIONAL (default: Url::to(['/notifications/web-push-notification/unsubscribe']))
                        'subscribeLabel' => 'subscribe button label',       // OPTIONAL (default: 'Subscribe')
                        'unsubscribeLabel' => 'subscribe button label',     // OPTIONAL (default: 'Unsubscribe')
                    ],
                    'auth' => [
                        'VAPID' => [
                            'subject' => 'mailto:me@website.com',           // can be a mailto: or your website address
                            'publicKey' => '~88 chars',                     // (recommended) uncompressed public key P-256 encoded in Base64-URL
                            'privateKey' => '~44 chars',                    // (recommended) in fact the secret multiplier of the private key encoded in Base64-URL
                            'pemFile' => 'path/to/pem',                     // if you have a PEM file and can link to it on your filesystem
                            'pem' => 'pemFileContent',                      // if you have a PEM file and want to hardcode its content
                            'reuseVAPIDHeaders' => true                     // OPTIONAL (default: true) you can reuse the same JWT token them for the same flush session to boost performance using
                        ],
                    ],
                ],
            ],
        ],
    ],
];
```

To enable Web Push Notifications browsers is needed to verify your identity. A standard called VAPID can authenticate the application for all browsers. You'll need to create and provide a public and private key for your server. These keys must be safely stored and should not change.

If you want to disable the Web Push Notifications simply set the flag 'notifications.web.enable' to false.

In order to generate the uncompressed public and secret key, encoded in Base64, enter the following in your Linux bash:

```
$ openssl ecparam -genkey -name prime256v1 -out private_key.pem
$ openssl ec -in private_key.pem -pubout -outform DER|tail -c 65|base64|tr -d '=' |tr '/+' '_-' >> public_key.txt
$ openssl ec -in private_key.pem -outform DER|tail -c +8|head -c 32|base64|tr -d '=' |tr '/+' '_-' >> private_key.txt

```

Or you can use this method provided in the module:

```
\Minishlink\WebPush\VAPID::createVapidKeys();
```

### Create A Notification

[](#create-a-notification)

Each notification is represented by a single class (typically stored in the app/notifications directory).

```
namespace app\notifications;

use Yii;
use webzop\notifications\Notification;

class AccountNotification extends Notification
{
    const KEY_NEW_ACCOUNT = 'new_account';

    const KEY_RESET_PASSWORD = 'reset_password';

    /**
     * @var \yii\web\User the user object
     */
    public $user;

    /**
     * @inheritdoc
     */
    public function getTitle(){
        switch($this->key){
            case self::KEY_NEW_ACCOUNT:
                return Yii::t('app', 'New account {user} created', ['user' => '#'.$this->user->id]);
            case self::KEY_RESET_PASSWORD:
                return Yii::t('app', 'Instructions to reset the password');
        }
    }

    /**
     * @inheritdoc
     */
    public function getRoute(){
        return ['/users/edit', 'id' => $this->user->id];
    }
}
```

### Send A Notification

[](#send-a-notification)

Once the notification is created, you can send it as following:

```
$user = User::findOne(123);

AccountNotification::create(AccountNotification::KEY_RESET_PASSWORD, ['user' => $user])->send();
```

### Specifying Delivery Channels

[](#specifying-delivery-channels)

Every notification class has a shouldSend($channel) method that determines on which type of keys and channels the notification will be delivered. In this example, the notification will be delivered in all channels except "screen" or with key "new\_account":

```
/**
 * Get the notification's delivery channels.
 * @return boolean
 */
public function shouldSend($channel)
{
    if($channel->id == 'screen'){
        if(!in_array($this->key, [self::KEY_NEW_ACCOUNT])){
            return false;
        }
    }
    return parent::shouldSend($channel);
}
```

It is possible to limit the amount of same key/user notifications sent to a user through the `renotification_time` property of the notification. A new notification will be sent only if there aren't others with same user/key sent in the period specified in the property. `renotification_time` has to be a string accepted by [DateInterval::\_\_constructor()](https://php.net/manual/en/dateinterval.construct.php).

```
// For example to limit to 1 notification per hour
$notification = AccountNotification::create(AccountNotification::KEY_RESET_PASSWORD, ['user' => $user]);
$notification->renotification_time = 'PT1H';
$notification->send();
```

### Specifying The Send For Specific Channel

[](#specifying-the-send-for-specific-channel)

Every channel have a send method that receive a notification instance and define a way of that channel will send the notification. But you can override the send method by define toMail ("to" + \[Channel ID\]) in notification class. This example show how to do that:

```
/**
 * Override send to email channel
 *
 * @param $channel the email channel
 * @return void
 */
public function toEmail($channel){
    switch($this->key){
        case self::KEY_NEW_ACCOUNT:
            $subject = 'Welcome to MySite';
            $template = 'newAccount';
            break;
        case self::KEY_RESET_PASSWORD:
            $subject = 'Password reset for MySite';
            $template = 'resetPassword';
            break;
    }

    $message = $channel->mailer->compose($template, [
        'user' => $this->user,
        'notification' => $this,
    ]);
    Yii::configure($message, $channel->message);

    $message->setTo($this->user->email);
    $message->setSubject($subject);
    $message->send($channel->mailer);
}
```

### Custom Channels

[](#custom-channels)

This module have a some pre-built channels, but you may want to write your own channels to deliver notifications. To do that you need define a class that contains a send method:

```
namespace app\channels;

use webzop\notifications\Channel;
use webzop\notifications\Notification;

class VoiceChannel extends Channel
{
    /**
     * Send the given notification.
     *
     * @param Notification $notification
     * @return void
     */
    public function send(Notification $notification)
    {
        // use $notification->getTitle() ou $notification->getDescription();
        // Send your notification in this channel...
    }

}
```

You also should configure the channel in you application config:

```
[
    'modules' => [
        'notifications' => [
            'class' => 'webzop\notifications\Module',
            'channels' => [
                'voice' => [
                    'class' => 'app\channels\VoiceChannel',
                ],
                //...
            ],
        ],
    ],
]
```

### Screen Channel

[](#screen-channel)

This channel is used to show small notifications as above image preview. The notifications will stored in database, so before using this channel, you have to run its migrations scripts:

```
./yii migrate/up --migrationPath=vendor/webzop/yii2-notifications/migrations/
```

So you can call the Notifications widget in your app layout to show generated notifications:

```

    ...

```

### Web Push Notification Channel

[](#web-push-notification-channel)

This channel is used to send web push notification to subscriber. Each notification subscription will be stored in the database, so before using this channel, you have to run its migrations scripts:

```
./yii migrate/up --migrationPath=vendor/webzop/yii2-notifications/migrations/
```

So you can call the Notifications widget in your app layout to show generated notifications:

```

```

You can customize the HTML template for the widget as follow. Setting template to false will hide all widget HTML and the browser will prompts the user to allow notifications. If you customize the HTML template remember to include a button with id 'js-web-push-subscribe-button':

```

     '...  ...'
    ]) ?>

```

Remember to place the service-worker.js file in the web root in order to serve the service worker when the WebNotifications widget is initialized.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~115 days

Total

2

Last Release

3019d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/61654129?v=4)[overplex](/maintainers/overplex)[@overplex](https://github.com/overplex)

---

Top Contributors

[![niciz](https://avatars.githubusercontent.com/u/7879049?v=4)](https://github.com/niciz "niciz (40 commits)")[![goncaloe](https://avatars.githubusercontent.com/u/615966?v=4)](https://github.com/goncaloe "goncaloe (29 commits)")[![liviuk2](https://avatars.githubusercontent.com/u/11991766?v=4)](https://github.com/liviuk2 "liviuk2 (12 commits)")[![overplex](https://avatars.githubusercontent.com/u/61654129?v=4)](https://github.com/overplex "overplex (2 commits)")[![Dezinger](https://avatars.githubusercontent.com/u/432272?v=4)](https://github.com/Dezinger "Dezinger (2 commits)")[![rustamwin](https://avatars.githubusercontent.com/u/16498265?v=4)](https://github.com/rustamwin "rustamwin (1 commits)")[![jkmssoft](https://avatars.githubusercontent.com/u/19529106?v=4)](https://github.com/jkmssoft "jkmssoft (1 commits)")[![lucastimotiofirmino](https://avatars.githubusercontent.com/u/50211797?v=4)](https://github.com/lucastimotiofirmino "lucastimotiofirmino (1 commits)")

---

Tags

yii2yii2-notificationsyii2-notifications-moduleyii2-manage-notifications

### Embed Badge

![Health badge](/badges/overplex-yii2-notifications/health.svg)

```
[![Health](https://phpackages.com/badges/overplex-yii2-notifications/health.svg)](https://phpackages.com/packages/overplex-yii2-notifications)
```

###  Alternatives

[webzop/yii2-notifications

Notifications module for Yii2

72111.6k1](/packages/webzop-yii2-notifications)[loveorigami/yii2-notification-wrapper

This module for renders a message from session flash (with ajax, pjax support and etc.)

77199.7k5](/packages/loveorigami-yii2-notification-wrapper)[nterms/yii2-mailqueue

Email queue component for yii2 that works with yii2-swiftmailer.

87129.2k2](/packages/nterms-yii2-mailqueue)[tuyakhov/yii2-notifications

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.

6735.5k2](/packages/tuyakhov-yii2-notifications)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
