laravel-onesignal

repository·master·Indexed 19 days ago

https://github.com/berkayk/laravel-onesignal

A Laravel wrapper for the OneSignal API that simplifies sending push notifications to all users, specific users, segments, or users filtered by tags. It provides a OneSignal facade and OneSignalClient for managing player data, retrieving or deleting notifications, and sending custom notifications asynchronously.

Tokens
3.3K
Snippets
21
Records
21
Agent score
68%

What's inside laravel-onesignal

  1. Customize notification parameters with addParams and setParam

    master

    You can chain parameter settings to your notification calls. Use addParams($array) to pass multiple parameters at once, or setParam($key, $value) to set individual parameters fluently.

    use OneSignal;
    
    // Using addParams
    OneSignal::addParams(['android_accent_color' => 'FFCCAA72', 'small_icon' => 'ic_stat_default'])
        ->sendNotificationToAll("Message");
    
    // Using setParam chaining
    OneSignal::setParam('priority', 10)
        ->setParam('small_icon', 'ic_stat_onesignal_default')
        ->setParam('led_color', 'FFAACCAA')
        ->sendNotificationToAll("Message");
  2. Install the OneSignal Laravel package

    master

    To install the package, require it via Composer. If you are using a Laravel version older than 5.5, you must manually register the Service Provider and the OneSignal facade in config/app.php. Finally, publish the configuration file to create config/onesignal.php.

    composer require berkayk/onesignal-laravel
    
    # For Laravel < 5.5
    # Add Berkayk\OneSignal\OneSignalServiceProvider::class to 'providers'
    # Add 'OneSignal' => Berkayk\OneSignal\OneSignalFacade::class to 'aliases'
    
    php artisan vendor:publish --provider="Berkayk\OneSignal\OneSignalServiceProvider" --tag="config"
  3. Configure OneSignal credentials

    master

    Set up your OneSignal credentials in your .env file. You must provide the REST API URL, your App ID, and your REST API Key obtained from the OneSignal dashboard. You can also optionally set a Guzzle client timeout, which is recommended when sending notifications via Laravel queues.

    ONESIGNAL_REST_API_URL=https://api.onesignal.com
    ONESIGNAL_APP_ID=xxxxxxxxxxxxxxxxxxxx
    ONESIGNAL_REST_API_KEY=xxxxxxxxxxxxxxxxxx
    ONESIGNAL_GUZZLE_CLIENT_TIMEOUT=integer_value
  4. Configure asynchronous requests

    master

    You can enable asynchronous requests to prevent blocking the execution flow. When async(true) is called, methods like post, put, and delete will return a Guzzle Promise instead of the response.

    You can also attach a callback using callback() to execute logic once the promise resolves.

    $client->async(true)
           ->callback(function ($response) {
               // Handle response
           })
           ->sendNotificationToAll('Async message!');
  5. Initialize the OneSignalClient

    master

    To interact with the OneSignal API, instantiate the OneSignalClient class. You must provide your OneSignal App ID, REST API Key, and User Auth Key. You can optionally specify a Guzzle client timeout and a custom REST API URL.

    Parameters:

    • $appId: Your OneSignal App ID.
    • $restApiKey: Your OneSignal REST API Key.
    • $userAuthKey: Your OneSignal User Auth Key.
    • $guzzleClientTimeout (optional): Integer timeout for Guzzle requests.
    • $restApiUrl (optional): Custom REST API URL (defaults to config('onesignal.rest_api_url')).
    use Berkayk\OneSignal\OneSignalClient;
    
    $client = new OneSignalClient(
        'YOUR_APP_ID',
        'YOUR_REST_API_KEY',
        'YOUR_USER_AUTH_KEY'
    );
  6. Install and configure the OneSignal package

    master

    The package registers a onesignal singleton in the Laravel service container. To customize the configuration, you can publish the package's configuration file to your application's config directory using the config tag.

    After publishing, the package looks for configuration in the following order:

    1. config('services.onesignal')
    2. config('onesignal')
    3. config('onesignal::config')
    php artisan vendor:publish --tag="config"
  7. Send custom notifications

    master

    For full control over the OneSignal API parameters, use sendNotificationCustom($parameters). You can also send these asynchronously using OneSignal::async()->sendNotificationCustom($parameters).

    // Synchronous
    OneSignal::sendNotificationCustom($parameters);
    
    // Asynchronous
    OneSignal::async()->sendNotificationCustom($parameters);
  8. Send a notification to all users

    master

    Use OneSignal::sendNotificationToAll() to broadcast a message to every registered user. You can optionally include a URL for redirection, custom data, buttons, or a schedule.

    OneSignal::sendNotificationToAll(
        "Some Message", 
        $url = null, 
        $data = null, 
        $buttons = null, 
        $schedule = null
    );
  9. Send a notification to a specific user

    master

    Send a message to a single user using their unique OneSignal ID via OneSignal::sendNotificationToUser().

    OneSignal::sendNotificationToUser(
        "Some Message",
        $userId,
        $url = null,
        $data = null,
        $buttons = null,
        $schedule = null
    );
  10. Send a notification using Tags or Filters

    master

    Target specific users by passing an array of filter criteria to OneSignal::sendNotificationUsingTags(). Each filter is an array containing field (e.g., 'tag'), key, relation (e.g., '=', '>'), and value.

    // Example: Filter by specific email tags
    OneSignal::sendNotificationUsingTags(
        "Some Message",
        [
            ["field" => "tag", "key" => "email", "relation" => "=", "value" => "email21@example.com"],
            ["field" => "tag", "key" => "email", "relation" => "=", "value" => "email1@example.com"]
        ]
    );
    
    // Example: Filter by session count
    OneSignal::sendNotificationUsingTags(
        "Some Message",
        [
            ["field" => "tag", "key" => "session_count", "relation" => ">", "value" => '2'],
            ["field" => "tag", "key" => "first_session", "relation" => ">", "value" => '2000']
        ]
    );
  11. Send a notification to an external user

    master

    If you use custom external IDs (user IDs added by your application) instead of OneSignal's internal IDs, use OneSignal::sendNotificationToExternalUser().

    OneSignal::sendNotificationToExternalUser(
        "Some Message",
        $userId,
        $url = null,
        $data = null,
        $buttons = null,
        $schedule = null
    );