bluetea/push-notifications
PHP library by BlueTea for sending push notifications. Provides a lightweight foundation to integrate push messaging into your application and manage notification delivery across supported providers.
Installation:
composer require bluetea/push-notifications
(Note: Due to PHP 5.4 compatibility, ensure your project uses PHP 5.4+ or adjust your environment if needed.)
Basic Initialization:
use BlueTea\PushNotifications\PushNotification;
$push = new PushNotification([
'appId' => 'YOUR_ONESIGNAL_APP_ID',
'authToken' => 'YOUR_ONESIGNAL_AUTH_TOKEN',
'endpoint' => 'https://onesignal.com/api/v1/notifications' // Default
]);
First Use Case: Send a simple notification to all users:
$response = $push->send([
'contents' => ['en' => 'Hello from Laravel!'],
'include_player_ids' => ['player_id_1', 'player_id_2']
]);
src/BlueTea/PushNotifications/PushNotification.php: Core class methods (e.g., send(), sendToAll()).Sending Notifications:
$push->send([
'contents' => ['en' => 'Your order is shipped!'],
'include_player_ids' => [$user->device_token],
]);
$push->sendToAll(['contents' => ['en' => 'System update available!']]);
$push->sendToSegment('active_users', ['contents' => ['en' => 'Welcome back!']]);
Handling Responses:
$response = $push->send([...]);
if ($response->success()) {
// Log or process success
} else {
Log::error('Push failed:', ['error' => $response->getError()]);
}
Integration with Laravel:
AppServiceProvider:
$this->app->singleton('push', function ($app) {
return new PushNotification(config('services.onesignal'));
});
config/services.php):
'onesignal' => [
'appId' => env('ONESIGNAL_APP_ID'),
'authToken' => env('ONESIGNAL_AUTH_TOKEN'),
'endpoint' => env('ONESIGNAL_ENDPOINT', 'https://onesignal.com/api/v1/notifications'),
],
// app/Facades/PushNotification.php
class PushNotification extends Facade {
protected static function getFacadeAccessor() { return 'push'; }
}
Usage:
PushNotification::send([...]);
Event-Based Triggers:
OrderShipped) and dispatch notifications:
event(new OrderShipped($order));
// In listener:
PushNotification::send([...]);
Deprecated/Outdated:
array() instead of []). May require manual fixes for newer PHP/Laravel.release/v1.0.3.md for adjustments.Error Handling:
$response->success() or getError().try-catch for network issues:
try {
$push->send([...]);
} catch (\Exception $e) {
Log::error('Push notification failed', ['exception' => $e]);
}
Configuration Quirks:
Enable Logging:
$push = new PushNotification([...], true); // Enable debug mode (if supported)
(Note: Debug mode isn’t documented; inspect the class for logging hooks.)
Inspect Raw Responses:
$response = $push->send([...]);
dd($response->getRawResponse()); // Dump OneSignal's raw JSON
Test with Postman:
Manually test OneSignal endpoints using your appId/authToken to verify issues aren’t package-specific.
Custom Endpoints:
Override the endpoint in the constructor or extend the class:
class CustomPushNotification extends PushNotification {
public function __construct(array $config) {
$config['endpoint'] = 'https://custom-endpoint.com/api';
parent::__construct($config);
}
}
Add Metadata:
Extend the send() payload dynamically:
$push->send(array_merge([
'contents' => ['en' => 'Hello'],
'data' => ['custom_key' => 'value'], // Additional metadata
], $extraData));
Queue Jobs: Offload notifications to Laravel queues:
// In a job class:
public function handle() {
PushNotification::send([...]);
}
Dispatch with:
dispatch(new SendPushNotificationJob($payload));
Mocking for Tests: Use Laravel’s mocking to avoid real API calls:
$mock = Mockery::mock('overload:BlueTea\PushNotifications\PushNotification');
$mock->shouldReceive('send')->andReturn(new PushResponse(true));
How can I help you explore Laravel packages today?