From 302da5c1b0173b248e9e0b20264306e5fe5ae88a Mon Sep 17 00:00:00 2001 From: Prince Ali Khan Date: Mon, 26 Sep 2016 17:56:24 +0530 Subject: [PATCH 1/4] Support for including buttons in the notification Added support to included buttons in the notification. --- README.md | 9 ++++++--- src/OneSignalClient.php | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 012c4bf..68661f1 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", $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", $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/src/OneSignalClient.php b/src/OneSignalClient.php index c0dd097..51656e6 100644 --- a/src/OneSignalClient.php +++ b/src/OneSignalClient.php @@ -35,7 +35,7 @@ private function usesJSON() { $this->headers['headers']['Content-Type'] = 'application/json'; } - public function sendNotificationToUser($message, $userId, $url = null, $data = null) { + public function sendNotificationToUser($message, $userId, $url = null, $data = null, $buttons = null) { $contents = array( "en" => $message ); @@ -54,10 +54,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 ); @@ -76,10 +80,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 ); @@ -98,6 +106,10 @@ public function sendNotificationToSegment($message, $segment, $url = null, $data $params['data'] = $data; } + if (isset($button)) { + $params['buttons'] = $buttons; + } + $this->sendNotificationCustom($params); } @@ -120,6 +132,7 @@ public function sendNotificationCustom($parameters = []){ } $this->headers['body'] = json_encode($parameters); + $this->headers['buttons'] = json_encode($parameters); $this->headers['verify'] = false; return $this->post("notifications"); } From 7f5db5e62a396f2e5b735bea292a08879df950e3 Mon Sep 17 00:00:00 2001 From: Prince Ali Khan Date: Mon, 26 Sep 2016 21:28:52 +0530 Subject: [PATCH 2/4] Documentation Fix --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 68661f1..ae996ce 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ 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); + OneSignal::sendNotificationToAll("Some Message", $url, $data, $buttons); `$url` , `$data` and `$buttons` fields are exceptional. If you provide a `$url` parameter, users will be redirecting to that url. @@ -66,7 +66,7 @@ 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", $url, $data, $buttons); + 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` , `$data` and `$buttons` fields are exceptional. If you provide a `$url` parameter, users will be redirecting to that url. @@ -79,7 +79,7 @@ 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", $url, $data, $buttons); + OneSignal::sendNotificationToSegment("Some Message", $segment, $url, $data, $buttons); `$url` , `$data` and `$buttons` fields are exceptional. If you provide a `$url` parameter, users will be redirecting to that url. From e228f4501dff5674cffc162fbbcbe1de0f2e8592 Mon Sep 17 00:00:00 2001 From: Berkay Kaya Date: Wed, 19 Oct 2016 11:44:29 +0300 Subject: [PATCH 3/4] Fixed config typo. By default, php artisan vendor:publish should work fine in Laravel 5 applications. Lumen users may create onesignal config file manually. --- config/onesignal.php | 2 +- src/OneSignalServiceProvider.php | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) 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/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'); } } From 80607160960a6c5bbac46ecdf915358d580dc055 Mon Sep 17 00:00:00 2001 From: rigor Date: Tue, 22 Nov 2016 22:10:01 +0300 Subject: [PATCH 4/4] Custom optional parameters --- src/OneSignalClient.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/OneSignalClient.php b/src/OneSignalClient.php index 51656e6..c6b74d9 100644 --- a/src/OneSignalClient.php +++ b/src/OneSignalClient.php @@ -12,6 +12,7 @@ class OneSignalClient private $appId; private $restApiKey; private $userAuthKey; + private $additionalParams; public function __construct($appId, $restApiKey, $userAuthKey) { @@ -21,6 +22,7 @@ public function __construct($appId, $restApiKey, $userAuthKey) $this->client = new Client(); $this->headers = ['headers' => []]; + $this->additionalParams = []; } public function testCredentials() { @@ -35,6 +37,20 @@ private function usesJSON() { $this->headers['headers']['Content-Type'] = 'application/json'; } + 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 @@ -131,6 +147,8 @@ 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;