diff --git a/src/OneSignalClient.php b/src/OneSignalClient.php index 0171e22..b88b121 100644 --- a/src/OneSignalClient.php +++ b/src/OneSignalClient.php @@ -27,10 +27,83 @@ public function testCredentials() { return "APP ID: ".$this->appId." REST: ".$this->restApiKey; } - public function sendNotification($parameters = []){ + private function requiresAuth() { $this->headers['headers']['Authorization'] = 'Basic '.$this->restApiKey; + } + + private function usesJSON() { $this->headers['headers']['Content-Type'] = 'application/json'; + } + + public function sendNotificationToUser($message, $userId, $url = null, $data = null) { + $contents = array( + "en" => $message + ); + + $params = array( + 'app_id' => $this->appId, + 'contents' => $contents, + 'include_player_ids' => array($userId) + ); + + if (isset($url)) { + $params['url'] = $url; + } + + if (isset($data)) { + $params['data'] = $data; + } + + $this->sendNotificationCustom($params); + } + + public function sendNotificationToAll($message, $data = null) { + $contents = array( + "en" => $message + ); + + $params = array( + 'app_id' => $this->appId, + 'contents' => $contents, + 'included_segments' => array('All') + ); + + if (isset($data)) { + $params['data'] = $data; + } + + $this->sendNotificationCustom($params); + } + + public function sendNotificationToSegment($message, $segments, $data = null) { + $contents = array( + "en" => $message + ); + + $params = array( + 'app_id' => $this->appId, + 'contents' => $contents, + 'included_segments' => array('All') + ); + + if (isset($data)) { + $params['data'] = $data; + } + + $this->sendNotificationCustom($params); + } + + /** + * Send a notification with custom parameters defined in + * https://documentation.onesignal.com/v2.0/docs/notifications-create-notification + * @param array $parameters + * @return mixed + */ + public function sendNotificationCustom($parameters = []){ + $this->requiresAuth(); + $this->usesJSON(); $this->headers['body'] = json_encode($parameters); + $this->headers['verify'] = false; return $this->post("notifications"); } diff --git a/tests/test.php b/tests/test.php index 6e62fd4..2e7ea7a 100644 --- a/tests/test.php +++ b/tests/test.php @@ -10,4 +10,5 @@ getenv('REST_API_KEY'), getenv('USER_AUTH_KEY')); -echo $client->testCredentials(); \ No newline at end of file +echo $client->testCredentials(); +$client->sendNotificationToUser(".","4bc5da02-1722-4fee-943d-c8b5ccd507a2"); \ No newline at end of file