norkunas/onesignal-php-api

where to put this code??!

mfarhat437 opened this issue · 4 comments

require DIR . '/vendor/autoload.php';

use GuzzleHttp\Client as GuzzleClient;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use Http\Client\Common\HttpMethodsClient as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use OneSignal\Config;
use OneSignal\OneSignal;

$config = new Config();
$config->setApplicationId('your_application_id');
$config->setApplicationAuthKey('your_application_auth_key');
$config->setUserAuthKey('your_auth_key');

$guzzle = new GuzzleClient([ // http://docs.guzzlephp.org/en/stable/quickstart.html
// ..config
]);

$client = new HttpClient(new GuzzleAdapter($guzzle), new GuzzleMessageFactory());
$api = new OneSignal($config, $client);

In your application :)

Are you familiar with composer? Do you use any frameworks?

So bind the OneSignal service to your container:

use GuzzleHttp\Client as GuzzleClient;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use Http\Client\Common\HttpMethodsClient as HttpClient;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use OneSignal\Config;
use OneSignal\OneSignal;

$this->app->bind('OneSignal\OneSignal', function ($app) {
    $config = new Config();
    $config->setApplicationId('your_application_id');
    $config->setApplicationAuthKey('your_application_auth_key');
    $config->setUserAuthKey('your_auth_key');

    $client = new HttpClient(new GuzzleAdapter(new GuzzleClient()), new GuzzleMessageFactory());

    return new OneSignal\OneSignal($config, $client);
});

And then inject OneSignal to your controller. For more docs read https://laravel.com/docs/5.8/container