PHP code example of kce / onesignal-laravel

1. Go to this page and download the library: Download kce/onesignal-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kce / onesignal-laravel example snippets


return array (
    /*
     |--------------------------------------------------------------------------
     | One Signal App Id
     |--------------------------------------------------------------------------
     |
     | You can find in : Project > Settings > Key & ID's > ONESIGNAL APP ID
     |
     */
    'app_id' => env("ONESIGNAL_APP_ID", 'default_app_id'),

    /*
     |--------------------------------------------------------------------------
     | Rest API Key
     |--------------------------------------------------------------------------
     |
     | You can find in : Project > Settings > Key & ID's > REST API KEY
     |
     */
    'rest_api_key' => env("ONESIGNAL_REST_API_KEY", 'rest_api_key'),

    /*
     |--------------------------------------------------------------------------
     | User Auth Key
     |--------------------------------------------------------------------------
     |
     | You can find in : Profile > ACCOUNT & API KEYS > AUTH KEY
     |
     */
    'user_auth_key' => env("ONESIGNAL_USER_AUTH_KEY", 'user_auth_key'),
);

\KCE\OneSignal\Facades\OneSignalClient::sendToAll('Notification message');

\KCE\OneSignal\Facades\OneSignalClient::sendToCountry('Notification message', 'TR'); // Country ISO Code

\KCE\OneSignal\Facades\OneSignalClient::sendToLocation('Notification message', 10000, 37.4247, 41.33933); // Use Lat, Long and Radius

\KCE\OneSignal\Facades\OneSignalClient::sendToUser('Notification message', "player_id"); // Single player id

\KCE\OneSignal\Facades\OneSignalClient::sendToUser('Notification message', ["player_id1", 'player_id2]); // Multiple player ids

   \KCE\OneSignal\Facades\OneSignalClient::sendToSegment('Notification message', "segment");
   //or
   \KCE\OneSignal\Facades\OneSignalClient::sendToSegment('Notification message', ["segment", "segment2"]);
   

\KCE\OneSignal\Facades\OneSignalClient::sendToTags('Notification message', ["user_id", "=", 15]); //will send the notification to user that tagges as user_id 15

\KCE\OneSignal\Facades\OneSignalClient::setTitle("Notification Title")->setData([
        'key' => 'value'
    ])->sendToAll("Example Message");

\KCE\OneSignal\Facades\OneSignalClient::setSchedule("2018-10-29 10:00")->sendToAll("Cumhuriyet Bayramı Kutlu Olsun!");

\KCE\OneSignal\Facades\OneSignalClient::scheduleByUserTimezone("04:44PM")->sendToAll("This message will deliver based on user timezone on 04:44PM!");

$client = app('onesignal');
$client->addTag(['fav_color', 'green'])->addOrTag(['fav_color', 'red'])->sendToAll("Users like yellow or red");

$client = app('onesignal');
$client->addFilter('last_session', '>', '48', 'hours_ago')->sendToAll("Notification by last active"); // Users who last session time more than 48 Hours.
$client->addFilter('last_session', '<', '48', 'hours_ago')->sendToAll("Notification by last active"); // Users who last session time less than 48 Hours.
$client->addFilter('first_session', '>', '48', 'hours_ago')->sendToAll("Notification by last active"); // Users who first session time more than 48 Hours.
$client->addFilter('first_session', '<', '48', 'hours_ago')->sendToAll("Notification by last active"); // Users who last session time less than 48 Hours.


$client = app('onesignal');
$client
    ->setTitle([
             'en' => 'English Title',
             'tr' => 'Türkçe Başlık',
         ])
    ->sendToAll([
             'en' => 'English notification message',
             'tr' => 'Türkçe bildirim mesajı'
         ]);


$client = app('onesignal');

$client->addTag(['user_id', 15)->addTag('notifiable', 1)->setTitle("Test Notif")->sendToAll("New Message");