ddlzz/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
Configure app/config/config.yml for your target platform(s). Example for GCM (Android):
rms_push_notifications:
android:
gcm:
api_key: "YOUR_GCM_SERVER_KEY"
use_multi_curl: true
First Use Case
Inject the rms_push_notifications.notification_manager service and send a notification:
use RMS\PushNotificationsBundle\Notification\Notification;
$notification = new Notification();
$notification->setMessage("Hello from Laravel!")
->addDeviceToken("DEVICE_TOKEN_HERE");
$this->get('rms_push_notifications.notification_manager')->send($notification);
Notification Creation
Use the Notification class to craft messages:
$notification = new Notification();
$notification->setMessage("Your order is confirmed!")
->setTitle("Order Update")
->setSound("default")
->setBadge(1);
Targeting Devices Add device tokens (platform-specific):
$notification->addDeviceToken("ANDROID_TOKEN_1");
$notification->addDeviceToken("IOS_TOKEN_2");
Sending Notifications Dispatch via the manager:
$this->get('rms_push_notifications.notification_manager')->send($notification);
Batch Processing Loop through tokens and send in batches (e.g., for GCM):
foreach ($userTokens as $token) {
$notification->addDeviceToken($token);
if ($notification->getDeviceTokens()->count() >= 1000) {
$this->get('rms_push_notifications.notification_manager')->send($notification);
$notification->clearDeviceTokens();
}
}
Symfony Events
Trigger notifications via Symfony events (e.g., kernel.terminate for delayed sends):
$dispatcher->addListener('user.order_placed', function () use ($notificationManager) {
$notification = new Notification();
$notification->setMessage("Order placed!");
$notificationManager->send($notification);
});
Database Backing
Store device tokens in a devices table (e.g., user_id, platform, token) and fetch dynamically:
$tokens = Device::where('user_id', $userId)->pluck('token');
Custom Payloads
Extend the Notification class for platform-specific payloads (e.g., deep links):
class CustomNotification extends Notification {
public function setDeepLink($url) {
$this->payload['gcm']['data']['link'] = $url;
return $this;
}
}
Deprecated Services
symfony2.0 branch (not dev-master).Token Management
Rate Limits
Configuration Overrides
config.yml is merged correctly. Use parameters.yml for secrets:
parameters:
gcm_api_key: "%env(GCM_API_KEY)%"
Then reference in config.yml:
rms_push_notifications:
android:
gcm:
api_key: "%gcm_api_key%"
Enable Logging Configure Monolog to log failed sends:
monolog:
handlers:
push_notifications:
type: stream
path: "%kernel.logs_dir%/push_notifications.log"
level: debug
Then check logs for errors like:
{"error": "InvalidRegistration", "message": "Invalid token"}
Dry Runs
Test GCM with dry_run: true to validate payloads without sending:
rms_push_notifications:
android:
gcm:
api_key: "YOUR_KEY"
dry_run: true
Custom Platforms
Extend the bundle by creating a new Platform class (e.g., for FCM):
namespace AppBundle\Notification;
use RMS\PushNotificationsBundle\Platform\AbstractPlatform;
class FCMPlatform extends AbstractPlatform {
protected $endpoint = 'https://fcm.googleapis.com/fcm/send';
// Implement send() logic
}
Register as a service:
services:
app.push_notifications.fcm_platform:
class: AppBundle\Notification\FCMPlatform
tags:
- { name: rms_push_notifications.platform }
Notification Templates Create reusable templates via a factory:
class NotificationFactory {
public static function createOrderConfirmation($orderId) {
$notification = new Notification();
$notification->setTitle("Order #{$orderId}")
->setMessage("Your order is on the way!")
->setBadge(1);
return $notification;
}
}
Webhook Validation Validate push responses (e.g., GCM success/failure) and retry failed tokens:
$manager->send($notification, function ($response) {
if ($response->hasErrors()) {
$this->retryFailedTokens($response->getErrors());
}
});
How can I help you explore Laravel packages today?