quickcamx/devices

使用 Laravel 轻松管理移动设备和令牌

0.1 2023-02-28 21:03 UTC

This package is auto-updated.

Last update: 2024-09-09 01:40:00 UTC


README

使用 Laravel 轻松管理移动设备和令牌

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

轻松管理用户设备和设备令牌。

安装

您可以通过 composer 安装此包

composer require sfolador/devices

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="devices-config"

这是发布配置文件的内容

return [
     'allow_device_reassign' => false,
];

如果您将 allow_device_reassign 设置为 true,则可以为一个用户注册一个设备并将其分配给另一个用户。这通常发生在同一设备上的多账户的移动应用程序中。

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --tag="devices-migrations"
php artisan migrate

迁移将创建 Devices 表,其列将包括

  • id - 设备 ID
  • notifiable_id - "用户" ID
  • notifiable_type - "用户" 类型
  • name - 设备名称
  • type - 设备类型(移动,Web)
  • platform - 设备平台(iOS,Android,Web)
  • token - 设备令牌
  • created_at - 设备创建日期
  • updated_at - 设备更新日期

使用方法

您可以在 User 模型中使用 HasDevices 特性

use Sfolador\Devices\Models\Concerns\HasDevices;

class User extends Authenticatable
{
    use HasDevices;
}

此时可以检索用户的设备

$user = User::find(1);
$user->devices;

例如,从移动应用程序中注册新 Device,您可以使用提供的路由 POST /api/devices/attach

Route::post('/devices/attach', [DeviceController::class, 'attach']);

设备参数

注册设备需要带有以下参数的请求

$rules = [
    'platform' => ['required', new Enum(DevicePlatform::class)],
    'type' => ['required', new Enum(DeviceType::class)],
    'token' => 'required|string',
];

DevicePlatform 是一个 Enum,可以具有以下值:android、ios、web。DeviceType 是一个 Enum,可以具有以下值:mobile、web

Firebase 通知

如果需要,此包可以使用 kutia-software-company/larafirebase 通过 Firebase 发送推送通知(https://github.com/kutia-software-company/larafirebase),该软件已存在于 composer.json 中。

您需要通过运行以下命令发布 kutia-software-company/larafirebase 的配置

php artisan vendor:publish --provider="Kutia\Larafirebase\Providers\LarafirebaseServiceProvider" 

您将在配置文件夹中找到 larafirebase.php 文件。文件看起来像这样

return [

    'authentication_key' => null

];

请记住,您需要从 Firebase 获取 authentication_key

可通知配置

您需要通过添加 RouteNotifications 特性来设置您的可通知类

class User extends Model
{
    use HasDevices;
    use RoutesNotifications;

...

现在您可以向用户发送通知了

$user = User::find(1);
$user->notify(new \Sfolador\Devices\Notifications\FirebasePushNotification('title','message'));

如果您需要在通知字段上有更多自由度,您始终可以创建另一个 PushNotification 类或扩展 \Sfolador\Devices\Notifications\FirebasePushNotification

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近更改的更多信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全漏洞

请参阅 我们的安全策略 了解如何报告安全漏洞。

鸣谢

许可

MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。