Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Push::setCollapseKey() and ::setPriority() #60

Merged
merged 12 commits into from
Sep 7, 2024
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"require": {
"php": ">= 7.3",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.3 || ^7.0"
},
"require-dev": {
Expand Down
4 changes: 2 additions & 2 deletions src/Device/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(string $deviceId, bool $details = false)
*/
public function getUrl(): string
{
$url = "https://iid.googleapis.com/iid/info/{$this->deviceId}";
$url = "https://iid.googleapis.com/iid/info/$this->deviceId";

if ($this->details) {
$url .= '?details=true';
Expand All @@ -43,7 +43,7 @@ public function getUrl(): string
/**
* @inheritdoc
*/
public function getBody(): array
public function buildJsonBody(): array
{
return [];
}
Expand Down
2 changes: 1 addition & 1 deletion src/DeviceGroup/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(string $groupName, string $deviceId = '')
/**
* @inheritdoc
*/
public function getBody(): array
public function buildJsonBody(): array
{
return [
'operation' => 'create',
Expand Down
2 changes: 1 addition & 1 deletion src/DeviceGroup/Remove.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(string $notification_key_name, string $notification_
/**
* @inheritdoc
*/
public function getBody(): array
public function buildJsonBody(): array
{
return [
'operation' => 'remove',
Expand Down
2 changes: 1 addition & 1 deletion src/DeviceGroup/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(string $notification_key_name, string $notification_
/**
* @inheritdoc
*/
public function getBody(): array
public function buildJsonBody(): array
{
return [
'operation' => 'add',
Expand Down
9 changes: 6 additions & 3 deletions src/FcmClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Fcm\Device\Info;
use Fcm\Exception\FcmClientException;
use Fcm\Exception\NotificationException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use ReflectionClass;

/**
Expand Down Expand Up @@ -69,8 +71,9 @@ public static function create(string $apiToken, string $projectId, array $option

/**
* @param Request $request
*
* @return array
* @return array The response body received from the FCM API, as an associative array.
* @throws NotificationException When the given request isn't valid.
* @throws FcmClientException When the sending failed.
*/
public function send(Request $request): array
{
Expand All @@ -83,7 +86,7 @@ public function send(Request $request): array
// Create and send the request.
try {
$response = $client->post($url, [
'json' => $request->getBody()
'json' => $request->buildJsonBody()
]);
} catch (GuzzleException $e) {
throw new FcmClientException('Failed to Connect: '.$e->getMessage());
Expand Down
43 changes: 3 additions & 40 deletions src/Push/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
namespace Fcm\Push;

use Fcm\Exception\NotificationException;
use Fcm\Request;

class Data implements Request
class Data extends Push
{
use Push;

/**
* @param array $data
* @param string $recipient
Expand All @@ -27,46 +24,12 @@ public function __construct(array $data = [], string $recipient = '')
/**
* @inheritdoc
*/
public function getBody(): array
public function buildJsonBody(): array
{
if (empty($this->data)) {
throw new NotificationException('Data should not be empty for a Data Notification.');
}

if (empty($this->recipients) && empty($this->topics)) {
throw new NotificationException('Must minimaly specify a single recipient or topic.');
}

if (!empty($this->recipients) && !empty($this->topics)) {
throw new NotificationException('Must either specify a recipient or topic, not more then one.');
}

$request = [];

if (!empty($this->recipients)) {
if (\count($this->recipients) === 1) {
$request['to'] = current($this->recipients);
} else {
$request['registration_ids'] = $this->recipients;
}
}

if (!empty($this->topics)) {
$request['condition'] = array_reduce($this->topics, function ($carry, string $topic) {
$topicSyntax = "'%s' in topics";

if (end($this->topics) === $topic) {
return $carry .= sprintf($topicSyntax, $topic);
}

return $carry .= sprintf($topicSyntax, $topic) . '||';
});
}

if (!empty($this->data)) {
$request['data'] = $this->data;
}

return $request;
return parent::buildJsonBody();
}
}
86 changes: 21 additions & 65 deletions src/Push/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@

namespace Fcm\Push;

use Fcm\Exception\NotificationException;
use Fcm\Request;

class Notification implements Request
class Notification extends Push
{
use Push ;

/**
* @var string
*/
Expand All @@ -28,7 +23,7 @@ class Notification implements Request
* @var string
*/
private $icon;

/**
* @var string
*/
Expand All @@ -37,8 +32,8 @@ class Notification implements Request
/**
* @var string
*/
private $tag;
private $tag;

/**
* @var string
*/
Expand All @@ -53,12 +48,7 @@ class Notification implements Request
* @var string
*/
private $click_action;

/**
* @param string $title
* @param string $body
* @param string $recipient
*/

public function __construct(string $title = '', string $body = '', string $recipient = '', string $sound = '', string $icon = '', string $color = '', int $badge = 0, string $tag = '', string $subtitle = '', array $data = [], string $click_action = '')
{
$this->title = $title;
Expand All @@ -73,11 +63,11 @@ public function __construct(string $title = '', string $body = '', string $recip
if (!empty($click_action)) {
$this->click_action = $click_action;
}

if (!empty($data)) {
$this->data = $data;
}
}

if (!empty($recipient)) {
$this->addRecipient($recipient);
}
Expand Down Expand Up @@ -106,7 +96,7 @@ public function setBody(string $body): self

return $this;
}

/**
* @param string $sound
*
Expand All @@ -117,8 +107,8 @@ public function setSound(string $sound): self
$this->sound = $sound;

return $this;
}
}

/**
* @param string $icon
*
Expand All @@ -142,9 +132,9 @@ public function setColor(string $color): self

return $this;
}

/**
* @param string badge
* @param int $badge
*
* @return $this
*/
Expand All @@ -154,7 +144,7 @@ public function setBadge(int $badge): self

return $this;
}

/**
* @param string $tag
*
Expand Down Expand Up @@ -184,7 +174,8 @@ public function setSubtitle(string $subtitle): self
*
* @return $this
*/
public function setClickAction(string $click_action): self {
public function setClickAction(string $click_action): self
{
$this->click_action = $click_action;

return $this;
Expand All @@ -193,41 +184,9 @@ public function setClickAction(string $click_action): self {
/**
* @inheritdoc
*/
public function getBody(): array
public function buildJsonBody(): array
{
if (empty($this->recipients) && empty($this->topics)) {
throw new NotificationException('Must minimaly specify a single recipient or topic.');
}

if (!empty($this->recipients) && !empty($this->topics)) {
throw new NotificationException('Must either specify a recipient or topic, not more then one.');
}
/*
if (empty($this->data)) {
throw new NotificationException('Data should not be empty for a Data Notification.');
}
*/
$request = [];

if (!empty($this->recipients)) {
if (\count($this->recipients) === 1) {
$request['to'] = current($this->recipients);
} else {
$request['registration_ids'] = $this->recipients;
}
}

if (!empty($this->topics)) {
$request['condition'] = array_reduce($this->topics, function ($carry, string $topic) {
$topicSyntax = "'%s' in topics";

if (end($this->topics) === $topic) {
return $carry .= sprintf($topicSyntax, $topic);
}

return $carry .= sprintf($topicSyntax, $topic) . '||';
});
}
$request = parent::buildJsonBody();

$request['notification']['title'] = $this->title;
$request['notification']['body'] = $this->body;
Expand All @@ -236,17 +195,14 @@ public function getBody(): array
$request['notification']['color'] = $this->color;
$request['notification']['tag'] = $this->tag;
$request['notification']['subtitle'] = $this->subtitle;
if ($this->badge>0) {
if ($this->badge > 0) {
$request['notification']['badge'] = $this->badge;
}

if (!empty($this->click_action)) {
$request['notification']['click_action'] = $this->click_action;
}

if (!empty($this->data)) {
$request['data'] = $this->data;
}
return $request;

return $request;
}
}
Loading