Skip to content

Commit d001629

Browse files
committed
fix: ios() helper now sets 'apns' key, updated test to match review feedback
1 parent b3351ea commit d001629

File tree

2 files changed

+20
-73
lines changed

2 files changed

+20
-73
lines changed

src/FcmMessage.php

Lines changed: 16 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,7 @@ class FcmMessage implements Message
1212
{
1313
use Macroable;
1414

15-
/**
16-
* Extra android notification options (channel_id, sound, color, etc.).
17-
*/
18-
protected ?array $androidNotification = null;
19-
20-
/**
21-
* Create a new message instance.
22-
*/
15+
2316
public function __construct(
2417
public ?string $name = null,
2518
public ?string $token = null,
@@ -29,61 +22,45 @@ public function __construct(
2922
public array $custom = [],
3023
public ?Notification $notification = null,
3124
public ?Messaging $client = null,
32-
) {
33-
//
34-
}
25+
) { }
3526

36-
/**
37-
* Create a new message instance.
38-
*/
27+
3928
public static function create(...$args): static
4029
{
4130
return new static(...$args);
4231
}
4332

44-
/**
45-
* Set the message name.
46-
*/
33+
4734
public function name(?string $name): self
4835
{
4936
$this->name = $name;
5037

5138
return $this;
5239
}
5340

54-
/**
55-
* Set the message token.
56-
*/
41+
5742
public function token(?string $token): self
5843
{
5944
$this->token = $token;
6045

6146
return $this;
6247
}
6348

64-
/**
65-
* Set the message topic.
66-
*/
6749
public function topic(?string $topic): self
6850
{
6951
$this->topic = $topic;
7052

7153
return $this;
7254
}
7355

74-
/**
75-
* Set the message condition.
76-
*/
56+
7757
public function condition(?string $condition): self
7858
{
7959
$this->condition = $condition;
8060

8161
return $this;
8262
}
8363

84-
/**
85-
* Set the message data, or throw exception if data is not an array of strings.
86-
*/
8764
public function data(?array $data): self
8865
{
8966
if (! empty(array_filter($data, fn($value) => ! is_string($value)))) {
@@ -95,22 +72,20 @@ public function data(?array $data): self
9572
return $this;
9673
}
9774

98-
/**
99-
* Set additional custom message data.
100-
*/
75+
10176
public function custom(?array $custom = []): self
10277
{
10378
$this->custom = $custom ?? [];
10479

10580
return $this;
10681
}
107-
82+
10883
/**
109-
* Helper Android
84+
* Set android-specific custom options.
11085
*/
11186
public function android(array $options = []): self
11287
{
113-
// preserva lo que ya hubiera en custom y agrega/actualiza 'android'
88+
11489
$this->custom([
11590
...$this->custom,
11691
'android' => $options,
@@ -119,59 +94,38 @@ public function android(array $options = []): self
11994
return $this;
12095
}
12196

122-
97+
12398
/**
124-
* Helper iOS
99+
* Set APNs-specific custom options for iOS.
125100
*/
126101
public function ios(array $options = []): self
127102
{
128103
$this->custom([
129104
...$this->custom,
130-
'ios' => $options,
105+
'apns' => $options,
131106
]);
132107

133108
return $this;
134109
}
135110

136-
/**
137-
* Set the message notification.
138-
*/
111+
139112
public function notification(Notification $notification): self
140113
{
141114
$this->notification = $notification;
142115

143116
return $this;
144117
}
145118

146-
/**
147-
* Set the message Firebase Messaging client instance.
148-
*/
149119
public function usingClient(Messaging $client): self
150120
{
151121
$this->client = $client;
152122

153123
return $this;
154124
}
155125

156-
/**
157-
* Helper to define android.notification options.
158-
*
159-
* Example:
160-
* ->androidNotification([
161-
* 'channel_id' => 'alertas-operativas',
162-
* 'sound' => 'default',
163-
* ])
164-
*/
165-
public function androidNotification(array $options): self
166-
{
167-
$this->androidNotification = $options;
168-
169-
return $this;
170-
}
171-
172126
public function toArray()
173127
{
174-
// payload base
128+
175129
$payload = array_filter([
176130
'name' => $this->name,
177131
'data' => $this->data,
@@ -182,14 +136,7 @@ public function toArray()
182136
...$this->custom,
183137
]);
184138

185-
// si usamos el helper, lo metemos en android.notification
186-
if (! empty($this->androidNotification)) {
187-
$payload['android'] = $payload['android'] ?? [];
188-
$payload['android']['notification'] = array_merge(
189-
$payload['android']['notification'] ?? [],
190-
$this->androidNotification
191-
);
192-
}
139+
193140

194141
return $payload;
195142
}

tests/FcmMessageHelpersTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public function test_appends_ios_options_into_custom()
3333
{
3434
$msg = FcmMessage::create()
3535
->ios(['payload' => ['aps' => ['sound' => 'default']]]);
36-
3736
$payload = $msg->toArray();
3837

39-
$this->assertArrayHasKey('ios', $payload);
40-
$this->assertArrayHasKey('payload', $payload['ios']);
41-
$this->assertEquals('default', $payload['ios']['payload']['aps']['sound']);
38+
$this->assertArrayHasKey('apns', $payload);
39+
$this->assertArrayHasKey('payload', $payload['apns']);
40+
$this->assertEquals('default', $payload['apns']['payload']['aps']['sound']);
4241
}
4342

4443

44+
4545
/**
4646
* Test that using the helpers does not overwrite existing custom keys.
4747
* Ensures merging does not erase pre-existing custom payload data.

0 commit comments

Comments
 (0)