Installation:
composer require b3da/pusher-bundle "dev-master"
Add to AppKernel.php:
new b3da\PusherBundle\b3daPusherBundle(),
Configure (config/packages/b3da_pusher.yaml):
b3da_pusher:
fcm:
server_key: "%env(FCM_SERVER_KEY)%"
apn:
passphrase: "%env(APN_PASSPHRASE)%"
cert_path: "%kernel.project_dir%/config/apn_cert.pem"
First Use Case: Send an FCM notification:
use b3da\PusherBundle\Model\Message;
use b3da\PusherBundle\Service\Android\Fcm;
$fcm = $this->get('b3da_pusher.android.fcm');
$message = new Message('Hello', 'Your notification body');
$fcm->notify('device_token_here', $message->composeAndroidFcmMessage());
$response = $fcm->getOutputAsObject();
Message Composition:
Use b3da\PusherBundle\Model\Message to create structured payloads:
$message = new Message(
'Title',
'Body',
'default', // sound
1 // notification ID
);
Service Injection: Inject services via Symfony’s DI container:
$fcm = $this->get('b3da_pusher.android.fcm');
$apn = $this->get('b3da_pusher.ios.apn');
Batch Notifications: Loop through recipients and send:
foreach ($deviceTokens as $token) {
$fcm->notify($token, $message->composeAndroidFcmMessage());
}
Error Handling:
Check $fcm->getOutputAsObject() for response metadata (e.g., success, failure).
Environment Variables:
Store keys in .env (e.g., FCM_SERVER_KEY=your_key_here) and reference via %env().
Custom Payloads:
Extend Message to add custom data:
$message->addData(['key' => 'value']);
APNs Environment:
Toggle between sandbox/production by updating server_url in config.
Logging:
Wrap notify() calls in try-catch to log failures:
try {
$fcm->notify($token, $payload);
} catch (\Exception $e) {
$this->logger->error('FCM Error: ' . $e->getMessage());
}
Deprecated GCM:
Avoid using gcm—migrate to fcm (Firebase Cloud Messaging) as GCM is obsolete.
APNs Certificate Path:
Ensure cert_path is relative to the project root (not public/ or vendor/).
Proxy Configuration:
If behind a proxy, set proxy in config (e.g., proxy: 'http://proxy:port').
Message Validation: FCM/APNs may reject malformed payloads. Validate with:
$fcm->validatePayload($payload); // Hypothetical; check bundle docs.
Rate Limits: APNs has strict rate limits (~240 messages/minute). Implement exponential backoff for retries.
Response Inspection: Dump raw responses to debug:
dump($fcm->getOutputAsString());
APNs Errors:
APNs returns numeric errors (e.g., 8 = "Missing Device Token"). Map these to user-friendly messages.
FCM Logging: Enable FCM debug mode (if supported) via config:
fcm:
debug: true
Custom Services:
Extend b3da\PusherBundle\Service\AbstractPusher to add new providers (e.g., WebPush).
Message Transformers:
Override compose*Message() methods in a custom Message class for platform-specific tweaks.
Event Listeners:
Dispatch events post-notification (e.g., pusher.notification.sent) using Symfony’s event system.
Queue Integration: Offload notifications to a queue (e.g., Symfony Messenger) for async processing:
$this->get('messenger')->dispatch(new SendPushNotification($token, $payload));
How can I help you explore Laravel packages today?