All Projects â†’ williamcruzme â†’ Laravel Fcm

williamcruzme / Laravel Fcm

Licence: mit
🌐 A Laravel package to send Push Notifications to one or many devices of the user.

Projects that are alternatives of or similar to Laravel Fcm

Laravel Fcm
Firebase Cloud Messaging (FCM) sender for Laravel
Stars: ✭ 129 (+130.36%)
Mutual labels:  firebase, laravel, notifications
Larametrics
A self-hosted metrics and notifications platform for Laravel apps
Stars: ✭ 517 (+823.21%)
Mutual labels:  laravel, notifications
Snooze
A package to simplify automating future notifications and reminders in Laravel
Stars: ✭ 515 (+819.64%)
Mutual labels:  laravel, notifications
Laravel Server Monitor
Don't let your servers just melt down
Stars: ✭ 595 (+962.5%)
Mutual labels:  laravel, notifications
Laravel Onesignal
OneSignal Push Notifications for Laravel
Stars: ✭ 369 (+558.93%)
Mutual labels:  laravel, notifications
Laravel Firebase
A Laravel package for the Firebase PHP Admin SDK
Stars: ✭ 369 (+558.93%)
Mutual labels:  firebase, laravel
Laravel Failed Job Monitor
Get notified when a queued job fails
Stars: ✭ 582 (+939.29%)
Mutual labels:  laravel, notifications
Fcm
Firebase Cloud Messaging (FCM) notifications channel for Laravel
Stars: ✭ 169 (+201.79%)
Mutual labels:  firebase, laravel
Angularfire
The official Angular library for Firebase.
Stars: ✭ 7,029 (+12451.79%)
Mutual labels:  firebase, notifications
Laravel Smsgateway Notification Channel
SMS Gateway notification channel for Laravel
Stars: ✭ 13 (-76.79%)
Mutual labels:  laravel, notifications
Push Fcm Plugin
Official Firebase Cloud Messaging plugin for Push.js v1.0 đŸ”Ĩ
Stars: ✭ 37 (-33.93%)
Mutual labels:  firebase, notifications
Laravel Desktop Notifier
đŸ’ģ Send notifications to your desktop from your Laravel Artisan Commands. An JoliNotif wrapper for Laravel.
Stars: ✭ 333 (+494.64%)
Mutual labels:  laravel, notifications
Resources I Like
đŸ“šđŸ’¯ Collection of learning resources i like
Stars: ✭ 280 (+400%)
Mutual labels:  firebase, laravel
Webpush
Webpush notifications channel for Laravel.
Stars: ✭ 437 (+680.36%)
Mutual labels:  laravel, notifications
Todo Vue
Code for YouTube series on building a Todo App in Vue.js
Stars: ✭ 199 (+255.36%)
Mutual labels:  firebase, laravel
Learning Resources
"Technology Gold mine" to collect and share materials/resources
Stars: ✭ 573 (+923.21%)
Mutual labels:  firebase, laravel
Builder
Prepare your Laravel apps incredibly fast, with various commands, services, facades and boilerplates.
Stars: ✭ 1,009 (+1701.79%)
Mutual labels:  laravel, notifications
React Native Fcm
react native module for firebase cloud messaging and local notification
Stars: ✭ 1,729 (+2987.5%)
Mutual labels:  firebase, notifications
Awesome Opensource Apps
🏠ℹī¸ Curated list of awesome open source crafted web & mobile applications - Learn, Fork, Contribute & Most Importantly Enjoy!
Stars: ✭ 2,199 (+3826.79%)
Mutual labels:  firebase, laravel
Laravel Notify
Flexible Flash notifications for Laravel
Stars: ✭ 787 (+1305.36%)
Mutual labels:  laravel, notifications

Laravel FCM

Laravel GitHub


laravel-fcm is a powerful Laravel package to send Push Notifications to all devices of one or many users. Being channel-based you only need to specify the channel in your Laravel Notification.

Features

  • Easy integration
  • Compatible with any version of Laravel
  • Send notifications to all devices of one or many users at the same time
  • Send millions of notifications in batch
  • Fully customizable and adaptable
  • Queue support

📄 Content

đŸ’ŋ Installation

composer require williamcruzme/laravel-fcm

1. Configure the enviroment

Get the key of Server Key and paste in your .env file:
(gear-next-to-project-name) > Project Settings > Cloud Messaging

FCM_KEY=

2. Adding traits

In your App\Models\User model add the HasDevices trait:

<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use williamcruzme\FCM\Traits\HasDevices;

class User extends Authenticatable
{
    use Notifiable, HasDevices;
}

Remember, you are not limited to only including the trait on your App\Models\User model.

3. Running migrations

php artisan migrate

(Optional) Sometimes you may need to customize the migrations. Using the vendor:publish command you can export the migrations:

php artisan vendor:publish --tag=migrations

4. Adding routes

In your routes/api.php add the routes using the Device facade, this is for manage the devices:

Route::middleware('auth')->group(function () {
    Device::routes();
});

⚡ Create notification

1. Creating notifications

php artisan make:notification InvoicePaid

2. Adding delivery channels

Every notification class has a via method that determines on which channels the notification will be delivered. Add fcm as delivery channel:

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['fcm'];
}

3. Formatting notifications

The notification method support the Firebase payload:

/**
 * Get the Firebase Message representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \williamcruzme\FCM\Messages\FcmMessage
 */
public function toFcm($notifiable)
{
    return (new FcmMessage)
                ->notification([
                    'title' => 'Happy Code!',
                    'body' => 'This is a test',
                ]);
}

4. Sending notifications

FcmMessage automatically gets all devices of the notifiable entities; you just need to send notifications. Notifications may be sent in two ways: using the notify method of the Notifiable trait or using the Notification facade. First, let's explore using the trait:

Using The Notifiable Trait

This trait is utilized by the default App\User model and contains one method that may be used to send notifications: notify. The notify method expects to receive a notification instance:

use App\Notifications\InvoicePaid;

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

Remember, you may use the Illuminate\Notifications\Notifiable trait on any of your models. You are not limited to only including it on your App\User model.

Using The Notification Facade

Alternatively, you may send notifications via the Notification facade. This is useful primarily when you need to send a notification to multiple notifiable entities such as a collection of users. To send notifications using the facade, pass all of the notifiable entities and the notification instance to the send method:

use App\Notifications\InvoicePaid;
use Illuminate\Support\Facades\Notification;

Notification::send($users, new InvoicePaid($invoice));

🌐 Routes

These routes are generated automatically, once wherever you add Device::routes();

Add device

Method URI
POST /devices

Body Params

{
    "token": "fxssWy2Lgtk:APA91bFXy79AmofgTnBm5CfBpyeEFJsSHq0Xcdk..."
}

Remove device

Method URI
DELETE /devices

Body Params

{
    "token": "fxssWy2Lgtk:APA91bFXy79AmofgTnBm5CfBpyeEFJsSHq0Xcdk..."
}

🎨 Customizing The Notification

Specifying devices

Using the to method you can specific the device's to send the notification:

/**
 * Get the Firebase Message representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \williamcruzme\FCM\Messages\FcmMessage
 */
public function toFcm($notifiable)
{
    return (new FcmMessage)
                ->to('fxssWy2Lgtk:APA91bFXy79AmofgTnBm5CfBpyeEFJsSHq0Xcdk...')
                ->notification([
                    'title' => 'Happy Code!',
                    'body' => 'This is a test',
                ]);
}

Remember, this is optional because FcmMessage automatically gets all devices of $notifiable.

Specifying topics

Using the topic method you can specific the topic to send the notification:

/**
 * Get the Firebase Message representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \williamcruzme\FCM\Messages\FcmMessage
 */
public function toFcm($notifiable)
{
    return (new FcmMessage)
                ->topic('news')
                ->notification([
                    'title' => 'Happy Code!',
                    'body' => 'This is a test',
                ]);
}

This method ignores the devices of $notifiable.

Sending data

Using the data method you can specify the custom key-value pairs of the notification payload:

/**
 * Get the Firebase Message representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \williamcruzme\FCM\Messages\FcmMessage
 */
public function toFcm($notifiable)
{
    return (new FcmMessage)
                ->notification([
                    'title' => 'Happy Code!',
                    'body' => 'This is a test',
                ])
                ->data([
                    'type' => 'banner',
                    'link' => 'https://github.com/',
                ]);
}

Adding conditions

Using the condition method you can specify a boolean expression to send the notification:

/**
 * Get the Firebase Message representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \williamcruzme\FCM\Messages\FcmMessage
 */
public function toFcm($notifiable)
{
    return (new FcmMessage)
                ->condition("'stock-GOOG' in topics || 'industry-tech' in topics")
                ->notification([
                    'title' => 'Happy Code!',
                    'body' => 'This is a test',
                ]);
}

Setting priority

Using the priority method you can specify a priority of the notification. Default is normal:

/**
 * Get the Firebase Message representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \williamcruzme\FCM\Messages\FcmMessage
 */
public function toFcm($notifiable)
{
    return (new FcmMessage)
                ->priority('high')
                ->notification([
                    'title' => 'Happy Code!',
                    'body' => 'This is a test',
                ]);
}

🎨 Customizing The Controller

First of all, create your own DeviceController controller and add the ManageDevices trait.

Second, modify the namespace of the Device facade routes to :

Device::routes('App\Http\Controllers');

Custom request validations

The createRules deleteRules validationErrorMessages methods in the DeviceController allows you override the default request validations:

<?php

namespace App\Http\Controllers;

use williamcruzme\FCM\Traits\ManageDevices;

class DeviceController extends Controller
{
    use ManageDevices;
    
    /**
     * Get the validation rules that apply to the create a device.
     *
     * @return array
     */
    protected function createRules()
    {
        return [
            'token' => ['required', 'string'],
        ];
    }

    /**
     * Get the validation rules that apply to the delete a device.
     *
     * @return array
     */
    protected function deleteRules()
    {
        return [
            'token' => ['required', 'string', 'exists:devices,token'],
        ];
    }

    /**
     * Get the device management validation error messages.
     *
     * @return array
     */
    protected function validationErrorMessages()
    {
        return [];
    }
}

Custom response

The sendResponse and sendDestroyResponse method in the DeviceController allows you override the default response:

<?php

namespace App\Http\Controllers;

use williamcruzme\FCM\Traits\ManageDevices;

class DeviceController extends Controller
{
    use ManageDevices;
    
    /**
     * Get the response for a successful storing device.
     *
     * @param  williamcruzme\FCM\Device  $model
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendResponse($model)
    {
        return response()->json($model);
    }

    /**
     * Get the response for a successful deleting device.
     *
     * @param  williamcruzme\FCM\Device  $model
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendDestroyResponse($model)
    {
        return response()->json('', 204);
    }
}

Custom guards

The guard method in the DeviceController allows you override the default guard:

<?php

namespace App\Http\Controllers;

use williamcruzme\FCM\Traits\ManageDevices;

class DeviceController extends Controller
{
    use ManageDevices;
    
    /**
     * Get the guard to be used during device management.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return auth('admin')->guard();
    }
}

🚸 Contributing

You are welcome to contribute to this project, but before you do, please make sure you read the contribution guide.

🔒 License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].