meeeet-dev / larafirebase
Laravel Firebase Cloud Messaging (Larafirebase) with HTTP v1.
Requires
- google/apiclient: ^2.12
- illuminate/notifications: ^7.0|^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^7.0|^8.0|^9.0|^10.0|^11.0
README
Introduction
Larafirebase is a package thats offers you to send push notifications via Firebase in Laravel.
Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably deliver messages at no cost.
For use cases such as instant messaging, a message can transfer a payload of up to 4KB to a client app.
Installation
Follow the steps below to install the package.
Install via Composer
composer require meeeet-dev/larafirebase
Copy Configuration
Run the following command to publish the larafirebase.php
config file:
php artisan vendor:publish --provider="MeeeetDev\Larafirebase\Providers\LarafirebaseServiceProvider"
Configure larafirebase.php as needed
Open the larafirebase.php
configuration file, which you just published, and set the following values as needed:
project_id
: Replace with your actual Firebase project ID.firebase_credentials
: This refers to the JSON credentials file for your Firebase project. Make sure it points to the correct location in your project. This JSON file contains the authentication information for your Firebase project, allowing your Laravel application to interact with Firebase services. You can generate this JSON file in the Firebase Console. Once you have it, specify its path in this configuration.
Usage
Follow the steps below to find how to use the package.
Example usage in any class you want to use Larafirebase:
use MeeeetDev\Larafirebase\Facades\Larafirebase; class MyController { private $deviceTokens =['{TOKEN_1}', '{TOKEN_2}']; public function sendNotification() { return Larafirebase::withTitle('Hello World') ->withBody('I have something new to share with you!') ->withImage('https://firebase.google.com/images/social.png') ->withAdditionalData([ 'name' => 'wrench', 'mass' => '1.3kg', 'count' => '3' ]) ->sendNotification($this->deviceTokens); // Or return Larafirebase::fromRaw([ // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages "name" => "string", "data" => [ "string" => "string", ], "notification" => [ "object" => "(Notification)" ], "android" => [ "object" => "(AndroidConfig)" ], "webpush" => [ "object" => "(WebpushConfig)", ], "apns" => [ "object" => "(ApnsConfig)" ], "fcm_options" => [ "object" => "(FcmOptions)" ], "token" => "string", "topic" => "string", "condition" => "string" ])->sendNotification($this->deviceTokens); } }
Example usage in Notification class:
use Illuminate\Notifications\Notification; use MeeeetDev\Larafirebase\Messages\FirebaseMessage; class SendBirthdayReminder extends Notification { /** * Get the notification's delivery channels. */ public function via($notifiable) { return ['firebase']; } /** * Get the firebase representation of the notification. */ public function toFirebase($notifiable) { $deviceTokens = [ '{TOKEN_1}', '{TOKEN_2}' ]; return (new FirebaseMessage) ->withTitle('Hey, ', $notifiable->first_name) ->withBody('Happy Birthday!') ->asNotification($deviceTokens); // OR ->asMessage($deviceTokens); } }
Tips
- You can use
larafirebase()
helper instead of Facade.
Payload
Check how is formed payload to send to firebase:
Example 1:
Larafirebase::withTitle('Test Title')->withBody('Test body')->sendNotification('token1');
{ "token": "token1", "message" : { "notification": { "title": "Test Title", "body": "Test body" } }, }
Example 2:
Larafirebase::withTitle('Test Title')->withBody('Test body')->sendMessage('token1');
{ "token": "token1", "message" : { "data": { "title": "Test Title", "body": "Test body" } } }
If you want to create payload from scratch you can use method fromRaw
, for example:
return Larafirebase::fromRaw([ 'token' => 'token1', 'data' => [ 'key_1' => 'Value 1', 'key_2' => 'Value 2' ], 'android' => [ 'ttl' => '1000s', 'priority' => 'normal', 'notification' => [ 'key_1' => 'Value 1', 'key_2' => 'Value 2' ], ], ])->send();
Made with ♥ by Meeeet Dev (@meeeet-dev).