Installation
composer require norkunas/onesignal-php-api
Add to composer.json if using a custom package name.
First Use Case: Sending a Push Notification
use Norkunas\OneSignal\OneSignal;
$oneSignal = new OneSignal('YOUR_ONESIGNAL_APP_ID', 'YOUR_ONESIGNAL_REST_API_KEY');
$response = $oneSignal->sendNotification([
'app_id' => 'YOUR_ONESIGNAL_APP_ID',
'include_player_ids' => ['player_id_1', 'player_id_2'],
'contents' => ['en' => 'Hello from Laravel!'],
]);
Where to Look First
src/Norkunas/OneSignal/OneSignal.php for core methodstests/ for real-world usage examples$oneSignal->sendNotification([
'contents' => ['en' => 'Your message'],
'headings' => ['en' => 'Notification Title'],
'include_player_ids' => [$playerId],
]);
$oneSignal->sendNotification([
'filters' => [
['field' => 'tag', 'key' => 'user_type', 'relation' => '=', 'value' => 'premium'],
],
'contents' => ['en' => 'Exclusive offer!'],
]);
$oneSignal->createPlayer([
'player_id' => 'unique_id',
'external_user_id' => 'user_id_from_your_db',
'tags' => ['user_type' => 'premium'],
]);
$oneSignal->updatePlayerTags('player_id', ['new_tag' => 'value']);
if ($response->isSuccess()) {
$data = $response->getData();
} else {
$errors = $response->getErrors();
}
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(OneSignal::class, function ($app) {
return new OneSignal(
config('services.onesignal.app_id'),
config('services.onesignal.rest_key')
);
});
}
config/services.php)
'onesignal' => [
'app_id' => env('ONESIGNAL_APP_ID'),
'rest_key' => env('ONESIGNAL_REST_KEY'),
'timezone' => 'UTC',
],
$segments = ['segment1', 'segment2'];
$oneSignal->sendNotificationToSegments($segments, [
'contents' => ['en' => 'Batch message'],
]);
API Rate Limits
if ($attempts < 3) {
sleep(2 ** $attempts); // Exponential backoff
$response = $oneSignal->sendNotification(...);
}
Player ID vs. External ID
external_user_id for reliable targeting.Timezone Mismatches
$oneSignal->sendNotification([
'contents' => ['en' => 'Reminder'],
'send_after' => now()->addHours(2)->timestamp, // UTC timestamp
]);
Empty Responses
sendNotification() returns no data, check:
app_id and rest_key.include_player_ids or filters.Webhook Verification
$oneSignal->verifyWebhook($request->header('X-OneSignal-Key'), $request->getContent());
$oneSignal = new OneSignal($appId, $restKey, [
'logger' => new \Monolog\Logger('onesignal', [
new \Monolog\Handler\StreamHandler(storage_path('logs/onesignal.log')),
]),
]);
$response = $oneSignal->sendNotification(...);
\Log::debug('OneSignal Raw Response:', $response->getRawResponse());
Custom HTTP Client Override the default Guzzle client for retry logic or middleware:
$client = new \GuzzleHttp\Client(['timeout' => 30]);
$oneSignal = new OneSignal($appId, $restKey, ['http_client' => $client]);
Event Dispatching Trigger Laravel events after notifications:
$response = $oneSignal->sendNotification(...);
if ($response->isSuccess()) {
event(new NotificationSent($response->getData()));
}
Mocking for Tests Use a mock HTTP client in tests:
$mockClient = $this->createMock(\GuzzleHttp\Client::class);
$mockClient->method('post')->willReturn(new \GuzzleHttp\Psr7\Response(200, [], json_encode(['success' => true])));
$oneSignal = new OneSignal($appId, $restKey, ['http_client' => $mockClient]);
sendAfter for Scheduled Notifications
$oneSignal->sendNotification([
'contents' => ['en' => 'Scheduled message'],
'send_after' => strtotime('+1 hour'), // Unix timestamp
]);
data for Deep Links
$oneSignal->sendNotification([
'contents' => ['en' => 'Open app'],
'data' => ['screen' => 'home', 'id' => '123'],
]);
Handle in your app:
// In your deep link handler
$screen = $notification->data['screen'];
updatePlayersTags:
$oneSignal->updatePlayersTags([
'player_id_1' => ['tag1' => 'value1'],
'player_id_2' => ['tag2' => 'value2'],
]);
How can I help you explore Laravel packages today?