Installation
Run composer require digitalap/fcmhttpbundle in your Laravel project (note: this is a Symfony bundle, so you’ll need to adapt it for Laravel via Symfony Bridge or use it in a Symfony-based Laravel app like Lumen).
Configuration
Add the FCM API key to your .env:
FCM_API_KEY=your_server_key_here
Create a config file (e.g., config/fcm.php) to mirror the Symfony bundle’s YAML:
return [
'authentication_api_key' => env('FCM_API_KEY'),
];
First Use Case Send a basic notification to a single device:
use DigitalAp\FcmHttpBundle\Notification;
use DigitalAp\FcmHttpBundle\Message;
$notification = new Notification('Hello', 'This is a test notification');
$message = new Message(['device_token_here']);
$message->setNotification($notification);
// Inject the FCM sender service (see "Implementation Patterns" for DI setup)
$fcmSender = app('fcm_http.send');
$fcmSender->send($message);
Notification vs. Data Payload
Notification for user-facing alerts (title/body/sound).Data payload (via $message->setData(['key' => 'value'])) for background processing (e.g., syncing data).Message must have either a Notification or Data, but not both (FCM enforces this).Batching Tokens The bundle auto-batches tokens in chunks of 1,000 (FCM’s limit). For 1,500 tokens:
$message = new Message(range(1, 1500)); // Automatically split into 2 requests.
Service Integration
AppServiceProvider:
public function register() {
$this->app->bind('fcm_http.send', function ($app) {
return new \DigitalAp\FcmHttpBundle\Sender(
$app['config']['fcm.authentication_api_key']
);
});
}
public function sendNotification(FcmSender $fcmSender) {
// Use $fcmSender->send($message)
}
Customizing Headers
Extend the Sender class to add headers (e.g., for priority):
class CustomFcmSender extends \DigitalAp\FcmHttpBundle\Sender {
protected function getHeaders() {
$headers = parent::getHeaders();
$headers['X-Priority'] = 'high';
return $headers;
}
}
Deprecated Bundle
Sender class (e.g., adding Authorization: key=... header instead of Authorization: Bearer ...).getHeaders() method in a custom Sender class to use HTTP v1 format:
protected function getHeaders() {
return [
'Content-Type' => 'application/json',
'Authorization' => 'key=' . $this->apiKey,
];
}
Token Validation
device_tokens table).Error Handling
Sender to log errors:
public function send(Message $message) {
try {
$response = $this->httpClient->post($this->url, [
'headers' => $this->getHeaders(),
'body' => json_encode($message->toArray()),
]);
if ($response->getStatusCode() !== 200) {
throw new \RuntimeException($response->getBody());
}
} catch (\Exception $e) {
\Log::error('FCM Error: ' . $e->getMessage());
throw $e;
}
}
Configuration Quirks
fcm_http.autentication_api_key in YAML. Use config('fcm.authentication_api_key') in Laravel (adjust keys to match your config file).Testing
Use Firebase’s debug token (dY...) to test notifications without hitting rate limits.
Performance
Cache facade).Extending Functionality
Message class to support topics:
public function addTopic($topic) {
$this->data['topic'] = $topic;
}
$message->setCondition('"priority" in topics/"news" || "priority" == "high"');
Logging Log all FCM payloads for debugging:
\Log::debug('FCM Payload', ['payload' => $message->toArray()]);
How can I help you explore Laravel packages today?