Skip to content

Commit

Permalink
Added functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
berkayk committed Apr 29, 2016
1 parent 24b7548 commit 1664ab8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
75 changes: 74 additions & 1 deletion src/OneSignalClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
3 changes: 2 additions & 1 deletion tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
getenv('REST_API_KEY'),
getenv('USER_AUTH_KEY'));

echo $client->testCredentials();
echo $client->testCredentials();
$client->sendNotificationToUser(".","4bc5da02-1722-4fee-943d-c8b5ccd507a2");

0 comments on commit 1664ab8

Please sign in to comment.