digimantra/digi-notification

A Laravel package to send FCM push notification.

v1.0.2 2024-09-25 11:51 UTC

This package is not auto-updated.

Last update: 2025-03-12 14:02:01 UTC


README

Packagist Version Packagist Downloads GitHub License

This package provides an easy way to send Firebase Cloud Messaging (FCM) push notifications in Laravel applications using Google's Firebase API.

Table of Contents

Requirements

  • Laravel 8, 9, or 10
  • PHP 8.0 or higher
  • Google API Client (installed automatically via composer)
  • Firebase account with Cloud Messaging API enabled

Installation

  1. Install via Composer

    composer require digimantra/digi-notification
  2. Publish Configuration

    php artisan vendor:publish --tag=config --provider="DigiNotification\FcmHelper\FcmServiceProvider"
  3. Set Up Environment Variables

    Add the following to your .env file:

    FIREBASE_PROJECT_ID=your-firebase-project-id
    FIREBASE_CREDENTIALS=path/to/your/credentials.json

    You can generate the credentials file from the Firebase Console.

Usage

Sending FCM Notifications:

### Use Namespaces
use DigiNotification\FcmHelper\FcmHelper; 

### Assigning variables 
$tokens = ['device_token_1', 'device_token_2']; // Array of device tokens to which the notification will be sent.
$title = 'New Notification'; // The title of the notification.
$body = 'This is the body of the notification'; // The body content of the notification.
$data = ['key' => 'value']; // (Optional) Additional custom data.

### Example Send FCM notificaiton Dispatch
FcmHelper::sendFcmNotification($tokens, $title, $body, $data);  

Configuration

Queue Configuration

Ensure your queue system is set up properly by adding the following to your `.env` file:

env

QUEUE_CONNECTION=database

Then, create the jobs table and run the migration:

php artisan queue:table
php artisan migrate

Logging

To enable logging, add this to your .env file:

FCM_LOGGING=true
Set to false to disable logging.

Known Issues

Ensure FCM tokens are valid, as invalid tokens might cause delivery failures.
Invalid service account credentials will cause the package to fail to obtain an access token from Firebase.

License

This package is open-sourced under the MIT license.

Support

For support or more details you can reach out at it@digimantra.com.