PHP code example of brandonhudson / onesignal-php-api

1. Go to this page and download the library: Download brandonhudson/onesignal-php-api 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/ */

    

brandonhudson / onesignal-php-api example snippets




declare(strict_types=1);

use OneSignal\Config;
use OneSignal\OneSignal;
use Symfony\Component\HttpClient\Psr18Client;
use Nyholm\Psr7\Factory\Psr17Factory;

Psr17Factory();

$oneSignal = new OneSignal($config, $httpClient, $requestFactory, $streamFactory);

$myApps = $oneSignal->apps()->getAll();

$myApp = $oneSignal->apps()->getOne('application_id');

$newApp = $oneSignal->apps()->add([
    'name' => 'app name',
    'gcm_key' => 'key'
]);

$oneSignal->apps()->update('application_id', [
    'name' => 'new app name'
]);

$oneSignal->apps()->createSegment('application_id', [
    'name' => 'Segment Name',
    'filters' => [
        ['field' => 'session_count', 'relation' => '>', 'value' => 1],
        ['operator' => 'AND'],
        ['field' => 'tag', 'relation' => '!=', 'key' => 'tag_key', 'value' => '1'],
        ['operator' => 'OR'],
        ['field' => 'last_session', 'relation' => '<', 'value' => '30,'],
    ],
]);

$oneSignal->apps()->deleteSegment('application_id', 'segment_id');

$devices = $oneSignal->devices()->getAll();

$device = $oneSignal->devices()->getOne('device_id');

use OneSignal\Api\Devices;

$newDevice = $oneSignal->devices()->add([
    'device_type' => Devices::ANDROID,
    'identifier' => 'abcdefghijklmn',
]);

$oneSignal->devices()->update('device_id', [
    'session_count' => 2,
]);

$notifications = $oneSignal->notifications()->getAll();

$notification = $oneSignal->notifications()->getOne('notification_id');

$oneSignal->notifications()->add([
    'contents' => [
        'en' => 'Notification message'
    ],
    ''filters' => [
        [
            'field' => 'tag',
            'key' => 'is_vip',
            'relation' => '!=',
            'value' => 'true',
        ],
        [
            'operator' => 'OR',
        ],
        [
            'field' => 'tag',
            'key' => 'is_admin',
            'relation' => '=',
            'value' => 'true',
        ],
    ],
    // ..other options
]);

$oneSignal->notifications()->open('notification_id');

$oneSignal->notifications()->cancel('notification_id');

$oneSignal->notifications()->history('notification_id', [
    'events' => 'clicked', // or 'sent'
    'email' => '[email protected]',
]);

composer