Install the Bundle Add the package via Composer:
composer require draw/ionic-pusher-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Draw\IonicPusherBundle\IonicPusherBundle::class => ['all' => true],
];
Configure the Bundle Publish the default configuration:
php bin/console config:dump-reference | grep ionic_pusher
Or manually configure in config/packages/ionic_pusher.yaml:
ionic_pusher:
app_id: 'YOUR_IONIC_APP_ID'
api_key: 'YOUR_IONIC_API_KEY'
secret: 'YOUR_IONIC_SECRET'
environment: 'production' # or 'development'
First Use Case: Send a Push Notification
Inject the IonicPusher service and trigger a push:
use Draw\IonicPusherBundle\Service\IonicPusher;
class MyController extends AbstractController
{
public function __invoke(IonicPusher $pusher)
{
$pusher->sendPush(
'YOUR_DEVICE_TOKEN',
'Hello from Laravel!',
['custom' => 'data']
);
return new Response('Push sent!');
}
}
Sending Push Notifications
sendPush() with a device token, message, and optional payload:
$pusher->sendPush($deviceToken, 'Message', ['key' => 'value']);
foreach ($deviceTokens as $token) {
$pusher->sendPush($token, 'Update', ['urgent' => true]);
}
Event-Driven Pushes
KernelEvents::TERMINATE):
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;
$eventDispatcher->addListener(KernelEvents::TERMINATE, function (TerminateEvent $event) {
if ($event->getRequest()->isXmlHttpRequest()) {
$pusher->sendPush('token', 'New data available!');
}
});
Integration with Ionic Framework
Push service in your frontend to handle incoming pushes:
import { Push } from '@ionic-native/push/ngx';
constructor(private push: Push) {
this.push.initialize().then(() => {
this.push.on('notification').subscribe(data => {
console.log('Push received:', data);
});
});
}
Dynamic Device Token Storage
Store tokens in the database (e.g., users table) and fetch them when needed:
$tokens = User::whereNotNull('device_token')->pluck('device_token');
$pusher->sendPush($tokens, 'Broadcast message');
Conditional Pushes Use Symfony’s expression language to filter recipients:
$tokens = $userRepository->getTokensForCondition('active');
$pusher->sendPush($tokens, 'Conditional message');
Error Handling Wrap push calls in try-catch blocks to log failures:
try {
$pusher->sendPush($token, 'Message');
} catch (\Exception $e) {
$this->logger->error('Push failed: ' . $e->getMessage());
}
Environment Mismatches
ionic_pusher.environment matches your Ionic dashboard settings (e.g., development vs. production).Device Token Validity
Rate Limiting
RateLimiter or queue them with Symfony Messenger.Missing Dependencies
guzzlehttp/guzzle is installed (it’s a dependency, but verify).composer require guzzlehttp/guzzle if missing.Enable Verbose Logging
Add to config/packages/monolog.yaml:
handlers:
ionic_pusher:
type: stream
path: "%kernel.logs_dir%/ionic_pusher.log"
level: debug
Test with Postman Manually test the Ionic API endpoint to isolate issues:
POST https://push.ionic.io/api/v1/push
Headers: {
"X-Ionic-Application-Id": "YOUR_APP_ID",
"Authorization": "Basic YOUR_API_KEY"
}
Body: { "tokens": ["TOKEN"], "message": { "text": "Test" } }
Check Ionic Dashboard
Custom Payload Transformers
Override the default payload structure by extending the IonicPusher service:
use Draw\IonicPusherBundle\Service\IonicPusher;
class CustomIonicPusher extends IonicPusher
{
protected function transformPayload(array $payload): array
{
$payload['custom_key'] = 'custom_value';
return parent::transformPayload($payload);
}
}
Register the service in config/services.yaml:
services:
Draw\IonicPusherBundle\Service\IonicPusher:
class: App\Service\CustomIonicPusher
Queue Pushes for Async Processing Use Symfony Messenger to avoid timeouts:
use Symfony\Component\Messenger\MessageBusInterface;
$bus->dispatch(new SendPushMessage($token, 'Message', $payload));
Define the message and handler:
class SendPushMessage
{
public function __construct(
private string $token,
private string $message,
private array $payload
) {}
}
class SendPushHandler
{
public function __invoke(SendPushMessage $message, IonicPusher $pusher)
{
$pusher->sendPush($message->token, $message->message, $message->payload);
}
}
Webhook Integration Listen for Ionic push events (e.g., delivery reports) via webhooks:
use Symfony\Component\HttpFoundation\Request;
public function handleIonicWebhook(Request $request)
{
$data = json_decode($request->getContent(), true);
// Process event (e.g., log delivery status)
}
Configure the webhook URL in the Ionic dashboard.
How can I help you explore Laravel packages today?