|
2 | 2 |
|
3 | 3 | namespace RedjanYm\FCM;
|
4 | 4 |
|
5 |
| -class Notification extends Message implements \JsonSerializable |
| 5 | +use RedjanYm\FCM\Recipient\Recipient; |
| 6 | + |
| 7 | +class Notification implements \JsonSerializable |
6 | 8 | {
|
| 9 | + public Recipient $recipient; |
7 | 10 | 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 = []) |
28 | 31 | {
|
29 | 32 | $this->title = $title;
|
30 | 33 | $this->body = $body;
|
31 | 34 | $this->data = $data;
|
| 35 | + $this->recipient = $recipient; |
32 | 36 | }
|
33 | 37 |
|
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] |
125 | 39 | public function jsonSerialize()
|
126 | 40 | {
|
127 | 41 | return [
|
| 42 | + $this->recipient->getType() => $this->recipient->getTarget(), |
128 | 43 | 'notification' => [
|
129 | 44 | // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#notification
|
130 | 45 | 'title' => $this->title,
|
@@ -175,20 +90,22 @@ public function jsonSerialize()
|
175 | 90 | ...$this->extraFCMOptionsSettings,
|
176 | 91 | ],
|
177 | 92 | ],
|
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, |
190 | 106 | ],
|
191 |
| - ], |
| 107 | + $this->webPushHeadersSettings != [] ? ['headers' => $this->webPushHeadersSettings] : [], |
| 108 | + ), |
192 | 109 | 'fcm_options' => [
|
193 | 110 | // https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#fcmoptions
|
194 | 111 | 'analytics_label' => $this->analyticsLabel,
|
|
0 commit comments