authbucket/push-symfony-bundle
Since this is a Symfony bundle, Laravel integration requires Symfony Bridge or Laravel Symfony Integration (e.g., spatie/laravel-symfony). Start by:
Install Dependencies
composer require authbucket/push-symfony-bundle
composer require spatie/laravel-symfony # Bridge for Laravel
Register the Bundle
In config/app.php, add the bundle to config['extra.bundles'] under SymfonyBridge::getBundles():
'extra' => [
'bundles' => [
SymfonyBridge::getBundles([
AuthBucket\PushBundle\AuthBucketPushBundle::class,
]),
],
],
Configure Services Publish the bundle config:
php artisan vendor:publish --provider="AuthBucket\PushBundle\AuthBucketPushBundle"
Update config/push.php with your APNs/GCM credentials (e.g., API keys, certificates).
First Push Notification Use the service container to send a test notification:
use AuthBucket\PushBundle\Service\PushService;
$pushService = app(PushService::class);
$pushService->sendPush(
'device_token_here', // User's device token
'Hello from Laravel!', // Alert message
['custom_key' => 'value'] // Optional payload
);
Device Management
devices table with user_id, device_token, platform).$tokens = Device::where('user_id', auth()->id())->pluck('device_token');
Bulk Notifications
foreach ($tokens as $token) {
$pushService->sendPush($token, 'Your message');
}
Event-Driven Triggers
auth.login) to send welcome pushes:
use Illuminate\Support\Facades\Event;
Event::listen('auth.login', function ($user) {
$pushService->sendPush($user->device->token, 'Welcome back!');
});
Platform-Specific Logic
PushService to target APNs (iOS) or GCM (Android):
$pushService->setPlatform('apns'); // or 'gcm'
sendPush() in a job to avoid timeouts:
use AuthBucket\PushBundle\Jobs\SendPushJob;
SendPushJob::dispatch($token, 'Message')->onQueue('push');
PushService for custom logic:
$app->bind(PushService::class, function ($app) {
$service = new PushService($app['config']['push']);
$service->setLogger($app['log']); // Inject Laravel’s logger
return $service;
});
Symfony vs. Laravel Compatibility
ContainerInterface. Use spatie/laravel-symfony to bridge services.$app->singleton(PushService::class, function ($app) {
return new PushService($app['config']['push'], $app['log']);
});
Database Schema Mismatch
device table. Define a migration:
php artisan make:migration create_devices_table
Schema::create('devices', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->string('device_token')->unique();
$table->string('platform'); // 'apns' or 'gcm'
$table->timestamps();
});
Certificate Handling (APNs)
.pem files. Store them securely (e.g., Laravel’s storage/app) and reference in config/push.php:
'apns' => [
'certificate_path' => storage_path('app/certificates/auth_bucket.pem'),
'passphrase' => env('APNS_PASSPHRASE'),
],
Rate Limiting
throttle middleware or queue delays:
SendPushJob::dispatch($token, 'Message')->delay(now()->addMinutes(1));
config/push.php:
'logging' => [
'enabled' => true,
'channel' => 'push', // Laravel’s log channel
],
$pushService->setEnvironment('sandbox'); // APNs
$pushService->setEnvironment('development'); // GCM
$payload = json_encode(['alert' => 'Test'], JSON_UNESCAPED_UNICODE);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException('Invalid payload');
}
Custom Payloads
Extend the PushService to add metadata:
class CustomPushService extends PushService {
public function sendCustomPush($token, $message, array $metadata = []) {
$payload = array_merge([
'alert' => $message,
'metadata' => $metadata,
]);
return $this->sendPush($token, json_encode($payload));
}
}
Webhook Integration
Use Laravel’s Http client to handle push responses:
$response = Http::post('https://api.gcm.googleapis.com/...', [
'headers' => ['Authorization' => 'key=YOUR_KEY'],
'json' => ['registration_ids' => [$token], 'data' => ['message' => 'Hello']],
]);
Analytics
Track push success/failure in a push_events table:
event(new PushSent($token, $message, $response->successful()));
How can I help you explore Laravel packages today?