mirocow/yii2-notification

Yii2 notification

Installs: 7

Dependents: 0

Suggesters: 0

Security: 0

Stars: 8

Watchers: 5

Forks: 2

Open Issues: 1

Type:yii2-extension

dev-master 2019-10-23 01:30 UTC

This package is auto-updated.

Last update: 2024-11-23 12:26:38 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads License

Install

$ composer require --prefer-dist "mirocow/yii2-notification"
$ php ./yii migrate/up -p=@mirocow/notification/migrations

Configurate

    'modules' => [
        // Notification by providers
        'notification' => [
          'class' => 'mirocow\notification\Module',
          'providers' => [

              // SMS prostor-sms.ru
              'sms' => [
                'class' => 'mirocow\notification\providers\sms',
                'config' => [
                  'gate' => '',
                  'port' => 80,
                  'login' => '',
                  'password' => '',
                  'signature' => '',
                ]
              ],

              // E-mail
              'email' => [
                'class' => 'mirocow\notification\providers\email',
                'emailViewPath' => '@common/mail',
                'events' => [
                  'frontend\controllers\SiteController' => [
                    'Request',
                    'Signup',
                  ],
                  'backend\controllers\deal\SiteController' => [
                    'Login',
                    'Confirm',
                  ]
                ]                
              ]
          ],
        ]
    ],        

Using

By method send

use mirocow\notification\components\Notification;
    
    /* @var \mirocow\notification\Module $sender */
    $sender = Yii::$app->getModule('notification');
    
    $notification = new Notification([
      'from' => [\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'],
      'to' => $deal['userSeller']['email'], // строка или массив
      'toId' => $deal['userSeller']['id'], // строка или массив
      'phone' => $deal['userSeller']['phone_number'], // строка или массив
      'subject' => "\"{$deal['userBuyer']['nameForOut']}\" предлагает вам сделку для \"{$deal['ads']['product']->getName()}\"",
      'token' => 'TOKEN',
      'content' => "",
      'params' => [
        'productName' => $deal['ads']['product']->getName(),
        'avatar' => $deal['userBuyer']->avatarFile,
        'fromUserName' => $deal['userBuyer']['nameForOut'],
      ],
      'view' => ['html' => 'Request-html', 'text' => 'Request-text'],
      'path' => '@common/mail/deal',
      'notify' => ['growl', 'На Ваш email отправлено письмо для подтверждения'],
      'callback' => function(Provider $provider, $status){
        // Тут можно обработать ответ от провайдеров нотификаций
      }
    ]);
           
    $sender->sendEvent($notification);

By Event

use yii\base\Event;
use mirocow\notification\components\Notification;

$event = new Notification(['params' => [
  'from' => [\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'],
  'to' => $user->email,
  'subject' => 'Регистрация на сайте ' . \Yii::$app->name,
  'emailView' => ['html' => 'signUp-html', 'text' => 'signUp-text'],
  'user' => $user,
  'phone' => $user->phone_number,
  'notify' => ['growl', 'На Ваш email отправлено письмо для подтверждения'],
]]);
Notification::trigger(self::className(),'Signup', $event);

or full

$notification = new Notification([
  'from' => [\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'],
  'to' => $deal['userSeller']['email'], // строка или массив
  'toId' => $deal['userSeller']['id'], // строка или массив
  'phone' => $deal['userSeller']['phone_number'], // строка или массив
  'subject' => "\"{$deal['userBuyer']['nameForOut']}\" предлагает вам сделку для \"{$deal['ads']['product']->getName()}\"",
  'token' => 'TOKEN',
  'content' => "",
  'params' => [
    'productName' => $deal['ads']['product']->getName(),
    'avatar' => $deal['userBuyer']->avatarFile,
    'fromUserName' => $deal['userBuyer']['nameForOut'],
  ],
  'view' => ['html' => 'Request-html', 'text' => 'Request-text'],
  'path' => '@common/mail/deal',
  'notify' => ['growl', 'На Ваш email отправлено письмо для подтверждения'],
  'callback' => function(Provider $provider, $status){
    // Тут можно обработать ответ от провайдеров нотификаций
  }
]);
Notification::trigger(self::className(),'Request', $notification);

With mirocow/yii2-queue

    \Yii::$app->queue->getChannel()->push(new MessageModel([
        'worker' => 'notification',
        'method' => 'action',
        'arguments' => [
            'triggerClass' => self::class,
            'methodName' => 'Subscribe',
            'arguments' => [
                'param' => 'value'
            ],
        ],
    ]), 30);

Tests

$ ./vendor/bin/codecept -c ./vendor/mirocow/yii2-notification run unit