Notifications 本文未发布 发布文章

未匹配的标注

Definition

Notifications allows you to inform the user about a state changes in your application.
The Laravel notifications supports sending notifications across a variety channels (mail, SMS, Slack, Database…).
When using the Database channel the notifications will be stored in a database to be displayed in your client interface.
For more details refer to this link.

Principles

  • Containers MAY or MAY NOT have one or more Notification.
  • Ship may contain Application general Notifications.

Rules

  • All Notifications MUST extend from App\Ship\Parents\Notifications\Notification.

Folder Structure

- app
    - Containers
        - {container-name}
            - Notifications
                - UserRegisteredNotification.php
                - ...
    - Ship
        - Notifications
            - SystemFailureNotification.php
            - ...

Code Samples

Example: a simple Notification

<?php

namespace App\Containers\User\Notifications;

use App\Containers\User\Models\User;
use App\Ship\Parents\Notifications\Notification;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;

class BirthdayReminderNotification extends Notification implements ShouldQueue
{

    use Queueable;

    protected $notificationMessage;

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

    public function toArray($notifiable)
    {
        return [
            'content' => $this->notificationMessage,
        ];
    }

    public function toMail($notifiable)
    {
        // $notifiable is the object you want to notify "e.g. user"
        return (new MailMessage)
            ->subject("Hello World")
            ->line("Hi, $notifiable->name")
            ->line($this->notificationMessage);
    }

    public function toSms($notifiable)
    {
        // ...
    }

    // ...
}

Usage from an Action or Task:

Notifications can be sent from Actions or Tasks using the Notification Facade.

\Notification::send($user, new BirthdayReminderNotification($notificationMessage));

Alternatively you can use the Illuminate\Notifications\Notifiable trait on the notifiable object “e.g. User” and then call it as follow:

// get any user
$user = User::firstOrCreate([
    'name' => 'Mahmoud Zalt',
    'email' => 'mail@something.com',
    'phone' => '0096123456789',
]);

// call notify, found on the Notifiable trait
$user->notify(new BirthdayReminderNotification($notificationMessage));

Select Channels

To select a notification channel, apiato have the app/Ship/Configs/notification.php config file where you can define the array of supported channels “e.g. SMS, Email, WebPush..”, to be used for all your notifications.
If you wan to override the configuration for some notifications classes, or if you prefer to defined the channels within each notification class itself, you can override the via function public function via($notifiable) in the notification class and define your channels.
Checkout laravel notification channels for list of supported integrations.

Queueing a Notification

To queue a notification you should use Illuminate\Bus\Queueable and implement Illuminate\Contracts\Queue\ShouldQueue.

Use DB channel

Generally you need to generate the notification migration php artisan notifications:table, then run php artisan migrate, however just running the migration command will do the job, since Apiato already adds the _create_notifications_table.php in the default migrations files directory app/Ship/Migrations/.

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~