Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update read me #22

Open
Tchayo opened this issue Apr 24, 2019 · 2 comments
Open

update read me #22

Tchayo opened this issue Apr 24, 2019 · 2 comments

Comments

@Tchayo
Copy link

Tchayo commented Apr 24, 2019

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.

@makbeth
Copy link

makbeth commented May 23, 2019

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)

@alnazer
Copy link

alnazer commented Jun 3, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants