suver / yii2-notifications
您的通知
1.0.6
2016-10-17 03:59 UTC
Requires
This package is not auto-updated.
Last update: 2024-09-28 19:47:31 UTC
README
您的通知
安装
运行以下命令之一
php composer.phar require suver/yii2-notifications
或者添加
"suver/yii2-notifications": "*"
安装迁移
yii migrate --migrationPath=@vendor/suver/yii2-notifications/migrations
配置
将此模块添加到您的 modules
和 bootsrap
指令中
'bootstrap' => [
'notifications',
],
'modules' => [
'notifications' => [
'class' => 'suver\notifications\Module',
// or
//'detailViewWidget' => '\app\widgets\DetailView',
//'gridViewWidget' => '\app\widgets\GridView',
'identityClass' => '\app\models\User',
'channels' => [
[ // Email channel
'class' => '\suver\notifications\EmailChannel',
'init' => [
'from' => 'mail@example.com',
],
'config' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.yandex.ru',
'username' => 'mail@example.com',
'password' => '****',
'port' => '465',
'encryption' => 'ssl',
],
],
],
[ // Yii2 Flash channel
'class' => '\suver\notifications\FlashNotificationsChannel',
'init' => [
'key' => 'info',
],
'config' => [],
],
],
];
],
或者如果您不想包括具有访问规则配置的模块,您必须使用 as access
指令配置模块,如下所示
'bootstrap' => [
'notifications',
],
'modules' => [
'notifications' => [
'class' => 'suver\behavior\notifications\Module',
'identityClass' => '\app\models\User',
'channels' => [
[ // Email channel
'class' => '\suver\notifications\EmailChannel',
'init' => [
'from' => 'mail@example.com',
],
'config' => [
'class' => 'yii\swiftmailer\Mailer',
'transport' => [
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.yandex.ru',
'username' => 'mail@example.com',
'password' => '****',
'port' => '465',
'encryption' => 'ssl',
],
],
],
[ // Yii2 Flash channel
'class' => '\suver\notifications\FlashNotificationsChannel',
'init' => [
'key' => 'info',
],
'config' => [],
],
],
'as access' => [
'class' => 'yii\filters\AccessControl',
'rules' => [
[
'controllers'=>['notifications/default'],
'allow' => true,
'roles' => ['@']
],
[
'controllers'=>['notifications/list'],
'allow' => true,
'roles' => ['@']
],
[
'controllers'=>['notifications/template'],
'allow' => true,
'roles' => ['@']
],
]
]
],
],
如何使用
Notifications::get('news-add', 1) // Return new instance notification model from "news-add" template for user with id=1 ->setParams(['newsLink' => Url::to(['/news-feed/default/' . $event->sender->id], true)]) // set parrams for replace "{{newsLink}}" => /news-feed/default/1 ->send(); // send message. // Set new message body Notifications::get('news-add', 1)->setMessage($message) // Set new message body & new subject Notifications::get('news-add', 1)->setMessage($message)->setSubject($subject) // Set new message body & new subject & hold over message Notifications::get('news-add', 1)->setMessage($message)->setSubject($subject)->holdOver() // Select "flash-notifications" channel & set new message body & new subject & hold over message Notifications::get('news-add', 1)->setChannel('flash-notifications')->setMessage($message)->setSubject($subject)->holdOver() // Send notifications for all user selected channel // You must implements UserNotificationsInterface and return from getNotificationChannels() method channel array as ['email', 'flash-notifications', 'and', 'other', 'notify'] Notifications::get('news-add', 1)->sendUserChannels()
如何添加新的通知通道
配置您的通道
'channels' => [
[ // You channel
'class' => '\you\path\YouChannel',
'init' => [
// your init settings
],
'config' => [
// your transport settings
],
],
然后编写通道类,例如
namespace suver\notifications; use Yii; use suver\notifications\models\Notifications as NotificationsModel; /** * Class Notification * @package yii2-notifications */ class YouChannel implements ChannelInterface { public $class; public $config; public $init; public function __construct($app, $config, $init) { $this->config = $config; $this->init = $init; $app->components = [ // configure you needed components ]; } public function getChannelName() { return 'you-channel-name'; } public function init($config) { $this->config = $config; } public function send(NotificationsModel $object, $user) { $subject = $object->getSubject(); $message = $object->getMessage(); Yii::$app->session->setFlash($this->init['key'], $subject . " | " . $message); return true; } }