yii2-notifications icon indicating copy to clipboard operation
yii2-notifications copied to clipboard

update read me

Open Tchayo opened this issue 5 years ago • 2 comments

A per the current read me, the interface methods exportForMail() and exportForSms() do not exist but what is available is the exportFor($channel) function which expects the channel parameter and the broadcastOn() functions. This document does not reflect on the current modifications and because of that setting it up is a problem. So please update it coz I really need to use your great extension, just not certain how to. FYI examples on using the database channel would greatly help.

Tchayo avatar Apr 24 '19 10:04 Tchayo

If You want to use database channel (for notification of users in your app for example) you can implement NotifiableInterface in your User model class and use NotifiableTrait as it was described in readme. But also you must implement two methods in your User class: viaChannels() and routeNotificationFor($channel). like this:

class UserModel extends User implements \yii\web\IdentityInterface, NotifiableInterface
{
    use NotifiableTrait;

    const STATUS_INACTIVE = 0;
    const STATUS_ACTIVE = 1;

    public function viaChannels()
    {
        return ['database'];
    }

    public function routeNotificationFor($channel)
    {
        return [
            get_class($this),
            $this->id
        ];
    }

After that you can make any class that implements NotificationInterface like

class SendNotification implements NotificationInterface
{
    use NotificationTrait;

    private $text;

    public function __construct($text)
    {
        $this->text = $text;
    }


    public function exportForDatabase()
    {
        return \Yii::createObject([
            'class' => '\tuyakhov\notifications\messages\DatabaseMessage',
            'subject' => "Your invoice #123 has been paid",
            'body' => $this->text
        ]);
    }
}

That's all! When you want to send a notification use something like this

$user = UserModel::findOne(1);
        $notification = new SendNotification('123');
        Yii::$app->notifier->send($user, $notification);

When you want to read notification use an example from readme. I believe this information will help someone and save some time)

makbeth avatar May 23 '19 08:05 makbeth

Please Change

 public function exportForDatabase()
    {
        return \Yii::createObject([
            'class' => '\tuyakhov\notifications\messages\DatabaseChannel',
            'subject' => "Invoice has been paid",
            'body' => "Your invoice #{$this->invoice->id} has been paid",
            'data' => [
                'actionUrl' => ['href' => '/invoice/123/view', 'label' => 'View Details']
            ]
        ]);
    }

To

    {
        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']
            ]
        ]);
    }```

alnazer avatar Jun 03 '20 05:06 alnazer