sfolador/devices

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

0.1 2023-02-28 21:03 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 - 设备类型(移动,网页)
  • platform - 设备平台(iOS,Android,网页)
  • 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/larafirebasehttps://github.com/kutia-software-company/larafirebase),已经存在于 composer.json 中,通过 Firebase 发送推送通知。

通过运行以下命令发布 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)。有关更多信息,请参阅 许可证文件