diff --git a/README.md b/README.md index 012c4bf..ae996ce 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/config/onesignal.php b/config/onesignal.php index 878ac73..8eb3448 100644 --- a/config/onesignal.php +++ b/config/onesignal.php @@ -19,5 +19,5 @@ | */ 'rest_api_key' => 'YOUR-REST-API-KEY-HERE', - 'user_auth_key' => 'YOUR-' + 'user_auth_key' => 'YOUR-USER-AUTH-KEY' ); \ No newline at end of file diff --git a/src/OneSignalClient.php b/src/OneSignalClient.php index b2fa78d..0085360 100644 --- a/src/OneSignalClient.php +++ b/src/OneSignalClient.php @@ -16,6 +16,7 @@ class OneSignalClient private $appId; private $restApiKey; private $userAuthKey; + private $additionalParams; /** * @var bool @@ -58,6 +59,7 @@ public function __construct($appId, $restApiKey, $userAuthKey) $this->client = new Client(); $this->headers = ['headers' => []]; + $this->additionalParams = []; } public function testCredentials() { @@ -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 ); @@ -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 ); @@ -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 ); @@ -135,6 +159,10 @@ public function sendNotificationToSegment($message, $segment, $url = null, $data $params['data'] = $data; } + if (isset($button)) { + $params['buttons'] = $buttons; + } + $this->sendNotificationCustom($params); } @@ -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); } diff --git a/src/OneSignalServiceProvider.php b/src/OneSignalServiceProvider.php index b1698c1..50bca43 100644 --- a/src/OneSignalServiceProvider.php +++ b/src/OneSignalServiceProvider.php @@ -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'); } }