Php Laravel数据库通知在通知::send()上执行某些操作

Php Laravel数据库通知在通知::send()上执行某些操作,php,laravel,push-notification,Php,Laravel,Push Notification,调用Notification::send()时是否会触发事件? 我想在调用notification::send()时调用一个函数来发送推送通知 我正在创建一个评论功能,用户可以使用@username格式提及其他用户,并为提及的每个用户发送通知 这是我的CommentController.php use App\Models\Comment; use App\Models\User; use App\Notifications\UserMentioned; use Illuminate\Http\

调用
Notification::send()
时是否会触发事件? 我想在调用notification::send()时调用一个函数来发送推送通知

我正在创建一个评论功能,用户可以使用@username格式提及其他用户,并为提及的每个用户发送通知

这是我的
CommentController.php

use App\Models\Comment;
use App\Models\User;
use App\Notifications\UserMentioned;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;

class CommentController extends Controller
{
    public function store(Request $request)
    {
        $note = new Comment();
        $note->content = $request->content;
        $note->save();

        preg_match_all('/@([\w\-]+)/', $request->content, $matches);
        $username = $matches[1];
        $users = User::whereIn('username', $username)->get();

        Notification::send($users, new UserMentioned($request->content));
    }
}
use Illuminate\Notifications\Notification;

class UserMentioned extends Notification
{
    public function via($notifiable)
    {
        return ['database'];
    }

    public function toDatabase($notifiable)
    {
        return [
            'title' => 'You have been mentioned by ' . auth()->user()->name,
            'content' => $notifiable,
        ];
    }
}
这是我的通知
usernoted.php

use App\Models\Comment;
use App\Models\User;
use App\Notifications\UserMentioned;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;

class CommentController extends Controller
{
    public function store(Request $request)
    {
        $note = new Comment();
        $note->content = $request->content;
        $note->save();

        preg_match_all('/@([\w\-]+)/', $request->content, $matches);
        $username = $matches[1];
        $users = User::whereIn('username', $username)->get();

        Notification::send($users, new UserMentioned($request->content));
    }
}
use Illuminate\Notifications\Notification;

class UserMentioned extends Notification
{
    public function via($notifiable)
    {
        return ['database'];
    }

    public function toDatabase($notifiable)
    {
        return [
            'title' => 'You have been mentioned by ' . auth()->user()->name,
            'content' => $notifiable,
        ];
    }
}
这是我发送推送通知的功能。还有其他控制器。那么,在哪里可以为每个
通知::发送
调用此函数一次呢

public function sendNotificationFCM(array $deviceKey, String $title, String $body)
{
    $url = 'https://fcm.googleapis.com/fcm/send';
    $serverKey = 'my-server-key';

    $notification = [
        'title' => $title,
        'body' => $body,
        'sound' => 'default',
        'badge' => '1',
    ];

    $arrayToSend = [
        'registration_ids' => $deviceKey,
        'notification' => $notification,
        'priority' => 'normal',
    ];

    $json = json_encode($arrayToSend);
    $headers = [
        'Content-Type: application/json',
        'Authorization: key=' . $serverKey,
    ];

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

    curl_exec($curl);

    curl_close($curl);
}

文档中有一节专门讨论这个主题

您可以监听
light\Notifications\Events\NotificationSent
并启动任何您喜欢的类