From 09ed83bbf62c77529a9212c8130d86ac857c8c08 Mon Sep 17 00:00:00 2001 From: Stojan Kukrika Date: Thu, 19 Apr 2018 14:38:57 +0200 Subject: [PATCH] get playes, one player, edit and create new player on onesignal --- README.md | 29 +++++++++++++++++++++++++ src/OneSignalClient.php | 48 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5c9ed45..2185be1 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ You can send a custom message with OneSignal::sendNotificationCustom($parameters); ### Sending a Custom Notification + ### Sending a async Custom Notification You can send a async custom message with @@ -105,3 +106,31 @@ You can send a async custom message with Please refer to https://documentation.onesignal.com/reference for all customizable parameters. +### Create Player(add user into onesignal) +You can send request with + + OneSignal::createPlayer($parameters); + +Please refer to https://documentation.onesignal.com/reference#add-a-device for all customizable parameters. + +### Edit Player(edit user in onesignal) +You can send request editPlayer (add in array $parameters value with 'id' params) + + OneSignal::editPlayer($parameters); + +Please refer to https://documentation.onesignal.com/reference#edit-device for all customizable parameters. + +### View device +You can send request with + + OneSignal::getPlayer($device_id); + +Please refer to https://documentation.onesignal.com/reference#view-device for all customizable parameters. + +### Get all Players(users from onesignal) +You can send request with + + OneSignal::getAllPlayers($limit, $offset); + +Please refer to https://documentation.onesignal.com/reference#view-devices for all customizable parameters. + diff --git a/src/OneSignalClient.php b/src/OneSignalClient.php index 9826d77..caec94c 100644 --- a/src/OneSignalClient.php +++ b/src/OneSignalClient.php @@ -272,6 +272,35 @@ public function editPlayer(Array $parameters) { return $this->sendPlayer($parameters, 'PUT', self::ENDPOINT_PLAYERS . '/' . $parameters['id']); } + /** + * Get list of all users/players + * + * @param int $limit + * @param int $offset + * @return mixed + */ + public function getAllPlayers($limit=300, $offset=0) { + $params = array( + 'app_id' => $this->appId, + 'limit' => $limit, + 'offset' => $offset + ); + return $this->sendPlayer($params, 'GET', self::ENDPOINT_PLAYERS . '?' . http_build_query($params)); + } + + /** + * Get list of all users/players + * + * @param string $id + * @return mixed + */ + public function getPlayer($id) { + $params = array( + 'app_id' => $this->appId, + ); + return $this->sendPlayer($params, 'GET', self::ENDPOINT_PLAYERS . '/'.$id.'?' . http_build_query($params)); + } + /** * Create or update a by $method value * @@ -285,11 +314,15 @@ private function sendPlayer(Array $parameters, $method, $endpoint) $this->requiresAuth(); $this->usesJSON(); - $parameters['app_id'] = $this->appId; - $this->headers['body'] = json_encode($parameters); - $method = strtolower($method); - return $this->{$method}($endpoint); + + if ($method=='get') { + $this->headers['headers']['Authorization'] = 'Basic '.$this->restApiKey; + return $this->{$method}($endpoint,$this->headers); + }else{ + $this->headers['body'] = json_encode($parameters); + return $this->{$method}($endpoint); + } } public function post($endPoint) { @@ -307,4 +340,11 @@ 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); + } }