繁体   English   中英

Laravel通知发送

[英]Laravel Notification Send

我已经创建了通知类。当用户登录通知显示欢迎时。当我们发送通知鞋错误时,在此处对字符串 SurveyNotification代码调用成员函数routeNotificationFor()

<?php
namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use App\User;

class WelcomeToDStrokeTennis extends Notification
{
    use Queueable;

    protected $user;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {


    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->line('MyApp Welcomes You.')
            ->action('Login To MyApp',         'http://dstroketennis.com')
            ->line('Thank you for trusting MyApp!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
        //
        ];
    }
}

我的登录控制器,我们称之为通知发送

 public function login(Request $request)
{
    $this->validateLogin($request);
    Notification::send($request->toArray(), new WelcomeToSurvey($request->toArray()));
    // If the class is using the ThrottlesLogins trait, we can automatically throttle
    // the login attempts for this application. We'll key this by the username and
    // the IP address of the client making these requests into this application.
    if ($this->hasTooManyLoginAttempts($request)) {
        $this->fireLockoutEvent($request);

        return $this->sendLockoutResponse($request);
    }

    if ($this->attemptLogin($request)) {
        return $this->sendLoginResponse($request);
    }

    // If the login attempt was unsuccessful we will increment the number of attempts
    // to login and redirect the user back to the login form. Of course, when this
    // user surpasses their maximum number of attempts they will get locked out.
    $this->incrementLoginAttempts($request);

    return $this->sendFailedLoginResponse($request);
}

设置用户

首先,您需要设置一个用户(或另一个扩展Notification类的模型,以便通知知道“去哪里”。您可以通过构造函数或简单地使用

$user->notify(new WelcomeToSurvey($request->toArray()));

您可以通过$request对象或Auth::user()方法获取Auth::user()

提供电邮

您需要设置一封将通知发送到的电子邮件!

在您的User模型中添加以下内容:

public function routeNotificationForMail()
    {
        return $this->email_address; //You e-mail property here
    }

有关更多信息,请查看相应的文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM