bluetea/push-notifications-bundle
Installation Add the bundle via Composer (note: package is archived, use with caution):
composer require bluetea/push-notifications-bundle dev-master
Register in AppKernel.php:
new Bluetea\PushNotificationsBundle\BlueteaPushNotificationsBundle(),
Basic Configuration
Define endpoints in config.yml:
bluetea_push_notifications:
endpoints:
- { name: "firebase", url: "https://fcm.googleapis.com/fcm/send", auth_key: "%env(FIREBASE_KEY)%" }
First Use Case
Inject the PushNotificationService and send a notification:
use Bluetea\PushNotificationsBundle\Service\PushNotificationService;
class NotificationController extends Controller
{
public function sendNotification(PushNotificationService $pushService)
{
$notification = [
'to' => '/topics/news',
'notification' => [
'title' => 'Hello',
'body' => 'World!'
]
];
$pushService->send('firebase', $notification);
}
}
bluetea.push_notification.endpoint.# services.yml
services:
App\Service\CustomEndpoint:
tags:
- { name: bluetea.push_notification.endpoint, alias: "custom_endpoint" }
Dynamic Endpoint Routing
Use the PushNotificationService to route notifications to configured endpoints:
$pushService->send('firebase', $payload);
Batch Processing Queue notifications for async delivery (e.g., with Symfony Messenger):
$pushService->queue('firebase', $payload);
Template-Based Notifications Combine with Twig to generate dynamic payloads:
{# templates/notification.html.twig #}
{
"to": "{{ token }}",
"notification": {
"title": "{{ title }}",
"body": "{{ body|raw }}"
}
}
$payload = $this->twig->render('notification.html.twig', $context);
$pushService->send('firebase', json_decode($payload, true));
.env (e.g., FIREBASE_KEY).bluetea_push_notifications:
debug: true
try {
$pushService->send('firebase', $notification);
} catch (\Exception $e) {
$this->addFlash('error', 'Notification failed: ' . $e->getMessage());
}
Archived Package
symfony/messenger + firebase/php-jwt.composer require firebase/php-jwt symfony/messenger
Configuration Overrides
Payload Validation
if (!json_validate($payload)) {
throw new \InvalidArgumentException('Invalid JSON payload');
}
debug: true in config.auth_key is a valid Server Key (not Client Key).Custom Endpoints
Extend the Bluetea\PushNotificationsBundle\Endpoint\AbstractEndpoint class:
class CustomEndpoint extends AbstractEndpoint
{
protected function sendRequest($url, $data)
{
// Custom logic (e.g., retry logic, headers)
return $this->httpClient->post($url, [
'headers' => ['X-Custom-Header' => 'value'],
'body' => json_encode($data)
]);
}
}
Event Listeners
Subscribe to bluetea.push_notification.send events to modify payloads:
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Bluetea\PushNotificationsBundle\Event\PushNotificationEvent;
class NotificationSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'bluetea.push_notification.send' => 'onSendNotification'
];
}
public function onSendNotification(PushNotificationEvent $event)
{
$event->setPayload(array_merge($event->getPayload(), ['priority' => 'high']));
}
}
Response Handling
Override the onResponse method in custom endpoints to parse provider-specific responses (e.g., Firebase’s sendResponse):
protected function onResponse(\Psr\Http\Message\ResponseInterface $response)
{
$data = json_decode($response->getBody(), true);
if (isset($data['failure'])) {
throw new \RuntimeException('Failed to send: ' . $data['failure']);
}
}
$batch = ['registration_ids' => [$token1, $token2], 'data' => $payload];
$this->sendRequest($url, $batch);
access_token).
```markdown
## Migration Note
For new projects, consider:
- **Symfony Messenger** + **Firebase Admin SDK** for decoupled, scalable notifications.
- **Laravel Echo/Pusher** for real-time web push notifications.
How can I help you explore Laravel packages today?