nguyentrigiang/laravel-notification

一个用于管理符合jsonapi规范的通信通知的包

v1.4.1 2020-10-15 07:28 UTC

This package is auto-updated.

Last update: 2024-09-29 06:02:44 UTC


README

Build Status License

介绍

这是一个用于Laravel的简单通知包装库。它通过定义的方法简化了基本的通知流程。您可以向所有用户发送消息,也可以通知单个用户,管理通知、设备ID,并配置接收通知的时间。

安装

首先,您需要使用Composer要求该包

composer require nguyentrigiang/laravel-notification

该包将自动注册服务提供者。

然后,从您的命令行运行 php artisan vendor:publish --provider="GiangNT\LaravelNotification\NotificationServiceProvider" 以发布通知迁移文件。

最后,再次从命令行运行

php artisan migrate

配置

如果您使用Onesignal,则运行以下命令

php artisan vendor:publish --tag=config

以发布默认配置文件。这将发布一个名为onesignal.php的配置文件,其中包含您的OneSignal授权密钥。

您需要在您的应用程序配置目录中找到并填写onesignal.php文件。app_id是您的OneSignal App ID,rest_api_key是您的REST API密钥。

用法

准备您的模型

要将通知与模型关联,该模型必须实现以下接口和特性

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use GiangNT\LaravelNotification\HasPlayer;
use GiangNT\LaravelNotification\HasConfiguration;
use GiangNT\LaravelNotification\InteractsWithPlayer;
use GiangNT\LaravelNotification\InteractsWithConfiguration;
use Illuminate\Notifications\Notifiable;

class User extends Model implements HasPlayer, HasConfiguration
{
    use InteractsWithPlayer, InteractsWithConfiguration, Notifiable;
}

创建通知

php artisan make:notification InvoicePaid

扩展 GiangNT\LaravelNotification\BaseNotification 而不是 Illuminate\Notifications\Notification

use GiangNT\LaravelNotification\BaseNotification;

class UserRegisted extends BaseNotification
{
}

发送通知

use App\Notifications\InvoicePaid;

$user->notify(new InvoicePaid($invoice));

访问通知

$user = App\Models\User::find(1);

foreach ($user->notifications as $notification) {
    echo $notification->type;
}

与玩家交互

$user->addPlayer('5ea79c81-327f-4d8b-98b1-58dbd22a277b');
$players = $user->players;
$user->deletePlayer('5ea79c81-327f-4d8b-98b1-58dbd22a277b');
$user->clearPlayer();

与配置交互

$data = [
    'start_time' => '5:00',
    'end_time' => '21:59',
    'days_of_the_week' => ['Monday', 'Tuesday']
];
$user->addConfiguration($data);
$user->resetConfiguration();