ciricihq/rms-push-notifications-bundle
Installation Add the package via Composer:
composer require richsage/rms-push-notifications-bundle:dev-master
Enable the bundle in app/AppKernel.php:
new RMS\PushNotificationsBundle\RMSPushNotificationsBundle(),
Configuration
Define your platform-specific credentials in app/config/config.yml:
rms_push_notifications:
android:
gcm:
api_key: "YOUR_GCM_SERVER_KEY"
ios:
certificate_path: "%kernel.root_dir%/path/to/certificate.pem"
passphrase: "CERTIFICATE_PASSPHRASE"
First Use Case
Inject the PushNotificationService into a controller/service and send a notification:
use RMS\PushNotificationsBundle\Service\PushNotificationService;
class NotificationController extends Controller
{
public function sendNotification(PushNotificationService $pushService)
{
$notification = [
'message' => 'Hello from Laravel!',
'badge' => 1,
'sound' => 'default',
];
$pushService->sendToDevice('ios', 'DEVICE_TOKEN', $notification);
}
}
Device Registration Store device tokens (e.g., GCM/iOS tokens) in your database for later use:
// Example: Store token in DB after registration
$user->devices()->create([
'platform' => 'ios',
'token' => $request->get('device_token'),
]);
Batched Sending
Use the sendToMultipleDevices() method for bulk notifications:
$tokens = ['ios_token_1', 'ios_token_2'];
$pushService->sendToMultipleDevices('ios', $tokens, $notification);
Platform-Specific Customization Override default payloads per platform in config:
rms_push_notifications:
ios:
default_payload:
category: "alert"
content_available: true
Event Listeners Extend functionality via Symfony events (e.g., log failed sends):
// src/RMS/PushNotificationsBundle/EventListener/NotificationListener.php
public function onNotificationSent(NotificationSentEvent $event)
{
if (!$event->isSuccess()) {
$this->logger->error('Failed to send notification', [
'device' => $event->getDeviceToken(),
'error' => $event->getError(),
]);
}
}
Certificate Handling (iOS)
.pem certificate is unencrypted or provide the correct passphrase.certificate_path to avoid issues with Symfony’s %kernel.root_dir% resolution.GCM Multi-CURL
use_multi_curl if sending to <100 devices to avoid memory issues:
gcm:
use_multi_curl: false
Token Validation
if (!$this->deviceRepository->exists($token)) {
throw new \RuntimeException('Invalid device token');
}
Rate Limiting
try {
$pushService->sendToDevice('gcm', $token, $notification);
} catch (RateLimitExceededException $e) {
sleep(2 ** $attempt); // Exponential backoff
retry();
}
Enable Verbose Logging
Add to config.yml:
rms_push_notifications:
debug: true
Logs will include raw API responses for troubleshooting.
Dry Runs (GCM) Test without sending actual notifications:
gcm:
dry_run: true
Custom Payload Builders
Override the PayloadBuilder service to modify payloads dynamically:
// services.yml
rms_push_notifications.payload_builder:
class: AppBundle\Service\CustomPayloadBuilder
arguments: ["@rms_push_notifications.payload_builder.default"]
Platform-Specific Services Extend the bundle by adding new platform services (e.g., for Web Push):
// src/AppBundle/Service/WebPushService.php
class WebPushService extends AbstractPushService
{
protected $platform = 'web';
// ...
}
Event Dispatching
Listen for rms_push_notifications.send and rms_push_notifications.sent events to intercept or modify notifications.
How can I help you explore Laravel packages today?