symfony/firebase-notifier
Symfony Notifier bridge for Firebase Cloud Messaging. Configure via FIREBASE_DSN and send notifications with platform-specific options using AndroidNotification, IOSNotification, or WebNotification to customize icons, sounds, actions, and more.
Install the package (via Symfony Notifier bridge):
composer require symfony/notifier firebase/php-jwt
Note: Laravel lacks native Notifier support; use a custom bridge or Symfony’s Chatter directly.
Configure Firebase DSN in .env:
FIREBASE_DSN=firebase://PROJECT_ID:PRIVATE_KEY@default
First Notification (Android Example):
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Bridge\Firebase\Notification\AndroidNotification;
use Symfony\Component\Notifier\Chatter\ChatterInterface;
// Instantiate Chatter (Laravel: bind to service container)
$chatter = new ChatterInterface();
$message = new ChatMessage('Hello from Laravel!');
$message->options(
(new AndroidNotification('/topics/news'))
->title('Laravel Alert')
->icon('ic_notification')
);
$chatter->send($message);
Verify Setup:
Workflow:
AndroidNotification, IOSNotification, or WebNotification classes to target specific platforms.$iosOptions = (new IOSNotification())
->badge(5)
->sound('ping.aiff')
->mutableContent(true)
->category('MESSAGE_CATEGORY')
->addAction('reply', 'Reply', 'reply.php', 'default')
->addAction('archive', 'Archive', 'archive.php', 'archive');
$message->options($iosOptions);
Use Case: Broadcast to user segments (e.g., "premium_users").
$message->options(
(new AndroidNotification('/topics/premium_users'))
->priority('high')
);
Pattern: Use clickAction (Android) or url (Web/iOS) for deep links.
$androidOptions = (new AndroidNotification('/topics/news'))
->clickAction('OPEN_ACTIVITY_1')
->addData(['screen' => 'article', 'id' => 123]);
Workflow: Dynamically set options based on user/device data.
$options = $user->isIos()
? (new IOSNotification())->badge($user->unreadCount)
: (new AndroidNotification())->priority('high');
$message->options($options);
Pattern: Dispatch notifications via Laravel’s queue system.
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Bridge\Firebase\Notification\AndroidNotification;
class FirebaseAlert implements ShouldQueue
{
use Queueable;
public function send($notifiable)
{
$message = new ChatMessage('Your alert!');
$message->options(
(new AndroidNotification('/topics/alerts'))
->title('Urgent')
->priority('high')
);
app('firebase.chatter')->send($message);
}
}
ChatterInterface to Laravel’s container in AppServiceProvider:
$this->app->singleton('firebase.chatter', function () {
return new \Symfony\Component\Notifier\Chatter\Chatter(
new \Symfony\Component\Notifier\Transport\FirebaseTransport(
$_ENV['FIREBASE_DSN']
)
);
});
Pattern: Use Symfony’s TransportFactoryTestCase (deprecated in v7.2+) or mock the Chatter.
public function testFirebaseNotification()
{
$chatter = $this->createMock(ChatterInterface::class);
$chatter->expects($this->once())
->method('send')
->with($this->isInstanceOf(ChatMessage::class));
$this->app->instance('firebase.chatter', $chatter);
// Trigger notification logic...
}
Symfony vs. Laravel Ecosystem Mismatch:
Bus/Events systems don’t natively support Symfony’s ChatterInterface.trait UsesFirebaseNotifier
{
protected function sendFirebaseNotification(ChatMessage $message)
{
app('firebase.chatter')->send($message);
}
}
Firebase DSN Format:
firebase://USER:PASS@default is deprecated in favor of service account JSON keys.$transport = new FirebaseTransport(
new \Symfony\Component\Notifier\Bridge\Firebase\Transport\FirebaseTransportOptions(
'path/to/serviceAccount.json'
)
);
PHP Version Requirements:
v7.4.* or upgrade Laravel:
composer require symfony/firebase-notifier:^7.4
Payload Size Limits:
data payloads for large content and notification payloads for alerts:
$message->options(
(new AndroidNotification())
->setData(['large_data' => json_encode($data)])
->title('Short alert')
);
Topic Subscription Management:
$admin = app('firebase.admin');
$admin->messaging()->subscribeToTopic('user123', '/topics/news');
Web Push Limitations:
Enable Firebase Debugging:
FIREBASE_DEBUG=true in .env to log raw FCM responses.Validate Payloads:
Check Quotas:
Laravel Logs:
Chatter::send() in a try-catch to log failures:
try {
$chatter->send($message);
} catch (\Throwable $e) {
\Log::error('Firebase notification failed', ['error' => $e->getMessage()]);
}
Custom Notification Classes:
AndroidNotification, IOSNotification, or WebNotification to add platform-specific logic:
class CustomAndroidNotification extends AndroidNotification
{
public function setCustomKeyValue(string $key, $value): self
{
$this->data[$key] = $value;
return $this;
}
}
Transport Decorators:
FirebaseTransport to add retries or analytics:
class AnalyticsFirebaseTransport implements TransportInterface
{
private $decorated;
public function __construct(FirebaseTransport $transport)
{
$this->decorated = $transport;
}
public function send(ChatMessage $message): void
{
// Log analytics before sending
\Log::info('Sending Firebase notification', ['topic' => $message->options()->getTopic()]);
$this->decorated->send($message
How can I help you explore Laravel packages today?