Skip to content

Commit 0c386f3

Browse files
committed
Simplify Notification class, fixes and example file.
1 parent 59d22d1 commit 0c386f3

File tree

9 files changed

+76
-154
lines changed

9 files changed

+76
-154
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
vendor
2+
.php-version

.php-version

-1
This file was deleted.

examples/messaging.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
include_once '../vendor/autoload.php';
4+
5+
use RedjanYm\FCM\Client;
6+
use RedjanYm\FCM\Notification;
7+
use RedjanYm\FCM\Recipient\Device;
8+
9+
$serviceAccountPath = 'service-account.json';
10+
$testToken = '123456789';
11+
12+
$client = new Client($serviceAccountPath);
13+
$client->applyCredentials();
14+
15+
$recipient = new Device($testToken);
16+
17+
$notification = new Notification($recipient, 'Title', 'Body', ['key' => 'value']);
18+
19+
$response = $client->send($notification);
20+
21+
var_dump($response->getBody()->getContents());

src/Client.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ public function __construct(string $serviceAccountPath)
2525

2626
public function setServiceAccountPath(string $serviceAccountPath): self
2727
{
28-
$this->serviceAccountPath = $serviceAccountPath;
28+
if (file_exists($serviceAccountPath) === false) {
29+
throw new \InvalidArgumentException('Service account file does not exist!');
30+
}
2931

32+
$this->serviceAccountPath = $serviceAccountPath;
3033
return $this;
3134
}
3235

@@ -52,7 +55,7 @@ private function getAccessToken(): string
5255
/**
5356
* @throws \GuzzleHttp\Exception\RequestException
5457
*/
55-
public function send(Message $message): ResponseInterface
58+
public function send(Notification $message): ResponseInterface
5659
{
5760
return $this->guzzleClient->post(
5861
$this->getApiUrl(),

src/ClientInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface ClientInterface
88
{
99
public function setServiceAccountPath(string $serviceAccountPath): self;
1010

11-
public function send(Message $message): ResponseInterface;
11+
public function send(Notification $message): ResponseInterface;
1212

1313
public function applyCredentials(): self;
1414
}

src/Message.php

-25
This file was deleted.

src/Notification.php

+42-125
Original file line numberDiff line numberDiff line change
@@ -2,129 +2,44 @@
22

33
namespace RedjanYm\FCM;
44

5-
class Notification extends Message implements \JsonSerializable
5+
use RedjanYm\FCM\Recipient\Recipient;
6+
7+
class Notification implements \JsonSerializable
68
{
9+
public Recipient $recipient;
710
public ?string $title;
8-
private ?string $body;
9-
private array $data;
10-
private ?string $image;
11-
private string $androidPriority = 'normal';
12-
private int $apnsPriority = 10;
13-
private string $ttl = '3600s';
14-
private ?string $badge;
15-
private ?string $icon;
16-
private ?string $color;
17-
private ?string $sound;
18-
private bool $contentAvailable = true;
19-
private ?string $analyticsLabel;
20-
private ?string $clickAction;
21-
private ?string $androidChannelId;
22-
private array $extraNotificationSettings = [];
23-
private array $extraFCMOptionsSettings = [];
24-
private array $extraAPNSHeadersSettings = [];
25-
private array $webPushHeadersSettings = [];
26-
27-
public function __construct(string $title = '', string $body = '', array $data = [])
11+
public ?string $body;
12+
public array $data;
13+
public ?string $image = null;
14+
public string $androidPriority = 'normal';
15+
public string $apnsPriority = '10';
16+
public string $ttl = '3600s';
17+
public ?int $badge = 0;
18+
public ?string $icon = null;
19+
public ?string $color = null;
20+
public string $sound = '';
21+
public bool $contentAvailable = true;
22+
public ?string $analyticsLabel = null;
23+
public ?string $clickAction = null;
24+
public ?string $androidChannelId = null;
25+
public array $extraNotificationSettings = [];
26+
public array $extraFCMOptionsSettings = [];
27+
public array $extraAPNSHeadersSettings = [];
28+
public array $webPushHeadersSettings = [];
29+
30+
public function __construct(Recipient $recipient, string $title, ?string $body = null, array $data = [])
2831
{
2932
$this->title = $title;
3033
$this->body = $body;
3134
$this->data = $data;
35+
$this->recipient = $recipient;
3236
}
3337

34-
public function setTitle(?string $title): self
35-
{
36-
$this->title = $title;
37-
38-
return $this;
39-
}
40-
41-
public function setBody(?string $body): self
42-
{
43-
$this->body = $body;
44-
45-
return $this;
46-
}
47-
48-
public function setImage(?string $image): self
49-
{
50-
$this->image = $image;
51-
52-
return $this;
53-
}
54-
55-
public function setAndroidPriority(string $androidPriority): self
56-
{
57-
$this->androidPriority = $androidPriority;
58-
59-
return $this;
60-
}
61-
62-
public function setTtl(string $ttl): self
63-
{
64-
$this->ttl = $ttl;
65-
66-
return $this;
67-
}
68-
69-
public function setBadge(?string $badge): self
70-
{
71-
$this->badge = $badge;
72-
73-
return $this;
74-
}
75-
76-
public function setIcon(?string $icon): self
77-
{
78-
$this->icon = $icon;
79-
80-
return $this;
81-
}
82-
83-
public function setColor(?string $color): self
84-
{
85-
$this->color = $color;
86-
87-
return $this;
88-
}
89-
90-
public function setClickAction(?string $actionName): self
91-
{
92-
$this->clickAction = $actionName;
93-
94-
return $this;
95-
}
96-
97-
public function setSound(?string $sound): self
98-
{
99-
$this->sound = $sound;
100-
101-
return $this;
102-
}
103-
104-
public function setAndroidChannelId(string $androidChannelId): self
105-
{
106-
$this->androidChannelId = $androidChannelId;
107-
108-
return $this;
109-
}
110-
111-
public function setAnalyticsLabel(?string $analyticsLabel): self
112-
{
113-
$this->analyticsLabel = $analyticsLabel;
114-
115-
return $this;
116-
}
117-
118-
public function setContentAvailable(bool $contentAvailable): self
119-
{
120-
$this->contentAvailable = $contentAvailable;
121-
122-
return $this;
123-
}
124-
38+
#[\ReturnTypeWillChange]
12539
public function jsonSerialize()
12640
{
12741
return [
42+
$this->recipient->getType() => $this->recipient->getTarget(),
12843
'notification' => [
12944
// https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification
13045
'title' => $this->title,
@@ -175,20 +90,22 @@ public function jsonSerialize()
17590
...$this->extraFCMOptionsSettings,
17691
],
17792
],
178-
'webpush' => [
179-
// https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushconfig
180-
'headers' => $this->webPushHeadersSettings,
181-
'notification' => [
182-
'title' => $this->title,
183-
'body' => $this->body,
184-
'icon' => $this->icon,
185-
],
186-
'fcm_options' => [
187-
// https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushfcmoptions
188-
'analytics_label' => $this->analyticsLabel,
189-
...$this->extraFCMOptionsSettings,
93+
'webpush' => array_merge([
94+
// https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushconfig
95+
'notification' => [
96+
'title' => $this->title,
97+
'body' => $this->body,
98+
'icon' => $this->icon,
99+
],
100+
'fcm_options' => [
101+
// https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#webpushfcmoptions
102+
'analytics_label' => $this->analyticsLabel,
103+
...$this->extraFCMOptionsSettings,
104+
],
105+
'data' => $this->data,
190106
],
191-
],
107+
$this->webPushHeadersSettings != [] ? ['headers' => $this->webPushHeadersSettings] : [],
108+
),
192109
'fcm_options' => [
193110
// https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#fcmoptions
194111
'analytics_label' => $this->analyticsLabel,

src/Recipient/Device.php

+5
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ public function getTarget(): string
1515
{
1616
return $this->token;
1717
}
18+
19+
public function getType(): string
20+
{
21+
return 'topic';
22+
}
1823
}

src/Recipient/Recipient.php

+1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
interface Recipient
66
{
77
public function getTarget(): string;
8+
public function getType(): string;
89
}

0 commit comments

Comments
 (0)