mirocow /
Yii2 通知
dev-master
2019-10-23 01:30 UTC
Requires
- php: >=5.4.0
- develandoo/yii2-push-notification: ^1.0
- ladamalina/yii2-smsc: @dev
- yiisoft/yii2: >=2.0
- yiisoft/yii2-redis: ~2.0.0
Requires (Dev)
Suggests
- mirocow/yii2-queue: to use Queue server
This package is auto-updated.
Last update: 2024-09-23 11:58:10 UTC
README
安装
$ composer require --prefer-dist "mirocow/yii2-notification"
$ php ./yii migrate/up -p=@mirocow/notification/migrations
配置
'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', ] ] ] ], ] ],
使用
通过方法发送
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);
通过事件
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);
或完整地
$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);
与 mirocow/yii2-queue 一起
\Yii::$app->queue->getChannel()->push(new MessageModel([ 'worker' => 'notification', 'method' => 'action', 'arguments' => [ 'triggerClass' => self::class, 'methodName' => 'Subscribe', 'arguments' => [ 'param' => 'value' ], ], ]), 30);
测试
$ ./vendor/bin/codecept -c ./vendor/mirocow/yii2-notification run unit