Skip to content

Commit

Permalink
updates from original repository
Browse files Browse the repository at this point in the history
  • Loading branch information
stojankukrika committed Apr 25, 2018
1 parent 09ed83b commit d152251
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 14 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ You can easily send a message to all registered users with the command

You can send a message based on a set of tags with the command

OneSignal::sendNotificationUsingTags("Some Message", array("key" => "device_uuid", "relation" => "=", "value" => 123e4567-e89b-12d3-a456-426655440000), $url = null, $data = null, $buttons = null, $schedule = null);
#####Example 1:
OneSignal::sendNotificationUsingTags("Some Message", array(["field" => "email", "relation" => "=", "value" => "[email protected]"]), $url = null, $data = null, $buttons = null, $schedule = null);

#####Example 2:
OneSignal::sendNotificationUsingTags("Some Message", array(["field" => "session_count", "relation" => ">", "value" => '2']), $url = null, $data = null, $buttons = null, $schedule = null);

### Sending a Notification To A Specific User

Expand Down
118 changes: 105 additions & 13 deletions src/OneSignalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
namespace stojankukrika\OneSignal;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request as Psr7Request;
use GuzzleHttp\Psr7\Response as Psr7Response;


class OneSignalClient
{
Expand All @@ -23,6 +31,16 @@ class OneSignalClient
*/
public $requestAsync = false;

/**
* @var int
*/
public $maxRetries = 2;

/**
* @var int
*/
public $retryDelay = 500;

/**
* @var Callable
*/
Expand Down Expand Up @@ -57,11 +75,31 @@ public function __construct($appId, $restApiKey, $iconColor="")
$this->restApiKey = $restApiKey;
$this->iconColor = $iconColor;

$this->client = new Client();
$this->client = new Client([
'handler' => $this->createGuzzleHandler(),
]);
$this->headers = ['headers' => []];
$this->additionalParams = [];
}


private function createGuzzleHandler() {
return tap(HandlerStack::create(new CurlHandler()), function (HandlerStack $handlerStack) {
$handlerStack->push(Middleware::retry(function ($retries, Psr7Request $request, Psr7Response $response = null, RequestException $exception = null) {
if ($retries >= $this->maxRetries) {
return false;
}
if ($exception instanceof ConnectException) {
return true;
}
if ($response && $response->getStatusCode() >= 500) {
return true;
}
return false;
}), $this->retryDelay);
});
}

public function testCredentials() {
return "APP ID: ".$this->appId." REST: ".$this->restApiKey;
}
Expand All @@ -88,7 +126,7 @@ public function setParam($key, $value)
return $this;
}

public function sendNotificationToUser($message, $userId, $url = null, $data = null, $buttons = null, $schedule = null) {
public function sendNotificationToUser($message, $userId, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) {
$contents = array(
"en" => $message
);
Expand All @@ -98,7 +136,7 @@ public function sendNotificationToUser($message, $userId, $url = null, $data = n
'contents' => $contents,
'android_led_color' => $this->iconColor,
'android_accent_color' => $this->iconColor,
'include_player_ids' => array($userId)
'include_player_ids' => is_array($userId) ? $userId : array($userId)
);

if (isset($url)) {
Expand All @@ -117,10 +155,22 @@ public function sendNotificationToUser($message, $userId, $url = null, $data = n
$params['send_after'] = $schedule;
}

if(isset($headings)){
$params['headings'] = array(
"en" => $headings
);
}

if(isset($subtitle)){
$params['subtitle'] = array(
"en" => $subtitle
);
}

$this->sendNotificationCustom($params);
}

public function sendNotificationUsingTags($message, $tags, $url = null, $data = null, $buttons = null, $schedule = null) {
public function sendNotificationUsingTags($message, $tags, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) {
$contents = array(
"en" => $message
);
Expand All @@ -130,7 +180,7 @@ public function sendNotificationUsingTags($message, $tags, $url = null, $data =
'contents' => $contents,
'android_led_color' => $this->iconColor,
'android_accent_color' => $this->iconColor,
'tags' => $tags,
'filters' => $tags,
);

if (isset($url)) {
Expand All @@ -149,10 +199,22 @@ public function sendNotificationUsingTags($message, $tags, $url = null, $data =
$params['send_after'] = $schedule;
}

if(isset($headings)){
$params['headings'] = array(
"en" => $headings
);
}

if(isset($subtitle)){
$params['subtitle'] = array(
"en" => $subtitle
);
}

$this->sendNotificationCustom($params);
}

public function sendNotificationToAll($message, $url = null, $data = null, $buttons = null, $schedule = null) {
public function sendNotificationToAll($message, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) {
$contents = array(
"en" => $message
);
Expand Down Expand Up @@ -181,10 +243,21 @@ public function sendNotificationToAll($message, $url = null, $data = null, $butt
$params['send_after'] = $schedule;
}

if(isset($headings)){
$params['headings'] = array(
"en" => $headings
);
}

if(isset($subtitle)){
$params['subtitle'] = array(
"en" => $subtitle
);
}
$this->sendNotificationCustom($params);
}

public function sendNotificationToSegment($message, $segment, $url = null, $data = null, $buttons = null, $schedule = null) {
public function sendNotificationToSegment($message, $segment, $url = null, $data = null, $buttons = null, $schedule = null, $headings = null, $subtitle = null) {
$contents = array(
"en" => $message
);
Expand Down Expand Up @@ -213,6 +286,18 @@ public function sendNotificationToSegment($message, $segment, $url = null, $data
$params['send_after'] = $schedule;
}

if(isset($headings)){
$params['headings'] = array(
"en" => $headings
);
}

if(isset($subtitle)){
$params['subtitle'] = array(
"en" => $subtitle
);
}

$this->sendNotificationCustom($params);
}

Expand Down Expand Up @@ -248,6 +333,16 @@ public function sendNotificationCustom($parameters = []){
return $this->post(self::ENDPOINT_NOTIFICATIONS);
}

public function getNotification($notification_id, $app_id = null)
{
$this->requiresAuth();
$this->usesJSON();
if (!$app_id) {
$app_id = $this->appId;
}
return $this->get(self::ENDPOINT_NOTIFICATIONS . '/' . $notification_id . '?app_id=' . $app_id);
}

/**
* Creates a user/player
*
Expand Down Expand Up @@ -318,7 +413,7 @@ private function sendPlayer(Array $parameters, $method, $endpoint)

if ($method=='get') {
$this->headers['headers']['Authorization'] = 'Basic '.$this->restApiKey;
return $this->{$method}($endpoint,$this->headers);
return $this->{$method}($endpoint);
}else{
$this->headers['body'] = json_encode($parameters);
return $this->{$method}($endpoint);
Expand All @@ -341,10 +436,7 @@ public function put($endPoint) {
return $this->client->put(self::API_URL . $endPoint, $this->headers);
}

public function get($endPoint, $headers) {
$client = new Client([
'defaults' => $headers
]);
return $client->get(self::API_URL . $endPoint, $headers);
public function get($endPoint) {
return $this->client->get(self::API_URL . $endPoint, $this->headers);
}
}

0 comments on commit d152251

Please sign in to comment.