mowahed / firebase-notification
Laravel package for Firebase Cloud Messaging
2.0.0
2025-05-16 11:36 UTC
Requires
- php: >=8.0 <8.4
- laravel/framework: ^8.0|^9.0|^10.0|^11.0
README
A comprehensive Laravel package for sending Firebase Cloud Messaging (FCM) push notifications to devices, topics, and conditions.
Features
- Send notifications to individual devices
- Send notifications to topics
- Subscribe/unsubscribe devices from topics
- Send notifications based on conditions
- Comprehensive error logging
- Support for all FCM features (images, sounds, deep links, etc.)
Installation
- Install via Composer:
composer require mowahed/firebase-notification:^2.0 php artisan vendor:publish --tag=firebase-config
Add This in .env
FIREBASE_PROJECT_ID="your-project-id" FIREBASE_CREDENTIALS="storage/app/firebase/service-account-key.json" FIREBASE_LOGGING_ENABLED="true" FIREBASE_LOGGING_CHANNEL="firebase"
Add this in logging.php
'channels' => [ 'firebase' => [ 'driver' => 'single', 'path' => storage_path('logs/firebase.log'), 'level' => 'error', ], ],
2. Send Notification to Device
use Mowahed\FirebaseNotification\Facades\FirebaseNotification; $response = FirebaseNotification::sendToDevice('device_fcm_token', [ 'title' => 'Welcome!', 'body' => 'Thanks for installing our app!', 'link' => 'https://yourapp.com', 'sound' => 'default', 'image' => 'https://example.com/notification.png', 'priority' => 'high' ]);
3. Send Notification to Topic
FirebaseNotification::sendToTopic('news,' [ 'title' => 'Breaking News', 'body' => 'Something big just happened!', 'link' => 'https://yourapp.com/news', 'sound' => 'default' ]);
4. Subscribe Device to Topic
$response = FirebaseNotification::subscribeToTopic( ['device_token_1', 'device_token_2'], 'news' );
5. Send Notification to Condition
$response = FirebaseNotification::sendToCondition( "'sports' in topics || 'news' in topics", [ 'title' => 'Sports News', 'body' => 'Latest sports updates!', 'link' => 'https://yourapp.com/sports' ] );
6. Unsubscribe Device from Topic
$response = FirebaseNotification::unsubscribeFromTopic( ['device_token_1', 'device_token_2'], 'news' );