Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Maykonn committed Dec 8, 2016
2 parents 1883ef3 + b7fd760 commit 613441b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ You can easily send a message to all registered users with the command
OneSignal::sendNotificationToAll("Some Message");
OneSignal::sendNotificationToAll("Some Message", $url);
OneSignal::sendNotificationToAll("Some Message", $url, $data);
OneSignal::sendNotificationToAll("Some Message", $url, $data, $buttons);

`$url` and `$data` fields are exceptional. If you provide a `$url` parameter, users will be redirecting to that url.
`$url` , `$data` and `$buttons` fields are exceptional. If you provide a `$url` parameter, users will be redirecting to that url.


### Sending a Notification To A Specific User
Expand All @@ -65,9 +66,10 @@ After storing a user's tokens in a table, you can simply send a message with
OneSignal::sendNotificationToUser("Some Message", $userId);
OneSignal::sendNotificationToUser("Some Message", $userId, $url);
OneSignal::sendNotificationToUser("Some Message", $userId, $url, $data);
OneSignal::sendNotificationToUser("Some Message", $userId, $url, $data, $buttons);

`$userId` is the user's unique id where he/she is registered for notifications. Read https://documentation.onesignal.com/docs/website-sdk-api#getUserId for additional details.
`$url` and `$data` fields are exceptional. If you provide a `$url` parameter, users will be redirecting to that url.
`$url` , `$data` and `$buttons` fields are exceptional. If you provide a `$url` parameter, users will be redirecting to that url.


### Sending a Notification To Segment
Expand All @@ -77,8 +79,9 @@ You can simply send a notification to a specific segment with
OneSignal::sendNotificationToSegment("Some Message", $segment);
OneSignal::sendNotificationToSegment("Some Message", $segment, $url);
OneSignal::sendNotificationToSegment("Some Message", $segment, $url, $data);
OneSignal::sendNotificationToSegment("Some Message", $segment, $url, $data, $buttons);

`$url` and `$data` fields are exceptional. If you provide a `$url` parameter, users will be redirecting to that url.
`$url` , `$data` and `$buttons` fields are exceptional. If you provide a `$url` parameter, users will be redirecting to that url.

### Sending a Custom Notification

Expand Down
2 changes: 1 addition & 1 deletion config/onesignal.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
|
*/
'rest_api_key' => 'YOUR-REST-API-KEY-HERE',
'user_auth_key' => 'YOUR-'
'user_auth_key' => 'YOUR-USER-AUTH-KEY'
);
37 changes: 34 additions & 3 deletions src/OneSignalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class OneSignalClient
private $appId;
private $restApiKey;
private $userAuthKey;
private $additionalParams;

/**
* @var bool
Expand Down Expand Up @@ -58,6 +59,7 @@ public function __construct($appId, $restApiKey, $userAuthKey)

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

public function testCredentials() {
Expand All @@ -72,7 +74,21 @@ private function usesJSON() {
$this->headers['headers']['Content-Type'] = 'application/json';
}

public function sendNotificationToUser($message, $userId, $url = null, $data = null) {
public function addParams($params = [])
{
$this->additionalParams = $params;

return $this;
}

public function setParam($key, $value)
{
$this->additionalParams[$key] = $value;

return $this;
}

public function sendNotificationToUser($message, $userId, $url = null, $data = null, $buttons = null) {
$contents = array(
"en" => $message
);
Expand All @@ -91,10 +107,14 @@ public function sendNotificationToUser($message, $userId, $url = null, $data = n
$params['data'] = $data;
}

if (isset($button)) {
$params['buttons'] = $buttons;
}

$this->sendNotificationCustom($params);
}

public function sendNotificationToAll($message, $url = null, $data = null) {
public function sendNotificationToAll($message, $url = null, $data = null, $buttons = null) {
$contents = array(
"en" => $message
);
Expand All @@ -113,10 +133,14 @@ public function sendNotificationToAll($message, $url = null, $data = null) {
$params['data'] = $data;
}

if (isset($button)) {
$params['buttons'] = $buttons;
}

$this->sendNotificationCustom($params);
}

public function sendNotificationToSegment($message, $segment, $url = null, $data = null) {
public function sendNotificationToSegment($message, $segment, $url = null, $data = null, $buttons = null) {
$contents = array(
"en" => $message
);
Expand All @@ -135,6 +159,10 @@ public function sendNotificationToSegment($message, $segment, $url = null, $data
$params['data'] = $data;
}

if (isset($button)) {
$params['buttons'] = $buttons;
}

$this->sendNotificationCustom($params);
}

Expand All @@ -156,7 +184,10 @@ public function sendNotificationCustom($parameters = []){
$parameters['included_segments'] = ['all'];
}

$parameters = array_merge($parameters, $this->additionalParams);

$this->headers['body'] = json_encode($parameters);
$this->headers['buttons'] = json_encode($parameters);
$this->headers['verify'] = false;
return $this->post(self::ENDPOINT_NOTIFICATIONS);
}
Expand Down
13 changes: 6 additions & 7 deletions src/OneSignalServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ class OneSignalServiceProvider extends ServiceProvider
public function boot()
{
$configPath = __DIR__ . '/../config/onesignal.php';
$app = $this->app;

if (class_exists('Illuminate\Foundation\Application') && $app instanceof LaravelApplication && $app->runningInConsole()) {
$this->publishes([$configPath => config_path('onesignal.php')]);
$this->mergeConfigFrom($configPath, 'onesignal');
} else if ( class_exists('Laravel\Lumen\Application', false) ) {
$app->configure('onesignal');

$this->publishes([$configPath => config_path('onesignal.php')]);
$this->mergeConfigFrom($configPath, 'onesignal');

if ( class_exists('Laravel\Lumen\Application') ) {
$this->app->configure('onesignal');
}
}

Expand Down

0 comments on commit 613441b

Please sign in to comment.