sfolador/devices

Manage mobile devices and tokens easily with Laravel

0.1 2023-02-28 21:03 UTC

README

68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f446576696365732e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d73666f6c61646f7225324664657669636573267061747465726e3d617263686974656374267374796c653d7374796c655f31266465736372697074696f6e3d4d616e6167652b646576696365732b616e642b6465766963652b746f6b656e732b266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313030707826696d616765733d75706c6f6164

Manage mobile devices and tokens easily with Laravel

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

Easily manage devices and device tokens for your users.

Installation

You can install the package via composer:

composer require sfolador/devices

You can publish the config file with:

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

This is the contents of the published config file:

return [
     'allow_device_reassign' => false,
];

if you set allow_device_reassign to true, it will be possible to register a device for a user and then assign it to another user. This happens usually in mobile applications with multi-accounts on the same device.

You can publish and run the migrations with:

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

the migration will create the Devices table and its columns will be:

  • id - the device id
  • notifiable_id - the "user" id
  • notifiable_type - the "user" type
  • name - the device name
  • type - the device type (mobile, web)
  • platform - the device platform (ios, android, web)
  • token - the device token
  • created_at - the device creation date
  • updated_at - the device update date

Usage

It's possible to use the HasDevices trait in your User model:

use Sfolador\Devices\Models\Concerns\HasDevices;

class User extends Authenticatable
{
    use HasDevices;
}

At this point is possible to retrieve the devices of a user:

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

To register a new Device, for example from a mobile app, you can use the provided route POST /api/devices/attach:

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

Device parameters

The register a Device you need a request with these parameters:

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

DevicePlatform is an Enum that can have these values: android, ios, web. DeviceType is an Enum that can have these values: mobile, web

Firebase notifications

If needed, this package can send push notifications with Firebase through the use of : kutia-software-company/larafirebase (https://github.com/kutia-software-company/larafirebase) , already present in the composer.json.

You need to publish the configuration of kutia-software-company/larafirebase by launching:

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

you will find the larafirebase.php file in the config folder. The file looks like this:

return [

    'authentication_key' => null

];

Please remind that you need to obtain the authentication_key from Firebase

Notifiable configuration

You need to setup your notifiable class by adding the RouteNotifications trait:

class User extends Model
{
    use HasDevices;
    use RoutesNotifications;

...

You are now ready to send a notification to the user:

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

If you need more freedom on the notification fields you can always create another PushNotification class or extend \Sfolador\Devices\Notifications\FirebasePushNotification.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.