Skip to content

Commit af9d67b

Browse files
authored
Merge pull request #228 from Jgmdevelopers/feature/android-notification-helper
feat: add android notification helper
2 parents 7fe9746 + 2c7bdbf commit af9d67b

File tree

2 files changed

+79
-17
lines changed

2 files changed

+79
-17
lines changed

src/FcmMessage.php

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

15+
1516
/**
1617
* Create a new message instance.
1718
*/
@@ -28,6 +29,7 @@ public function __construct(
2829
//
2930
}
3031

32+
3133
/**
3234
* Create a new message instance.
3335
*/
@@ -36,6 +38,7 @@ public static function create(...$args): static
3638
return new static(...$args);
3739
}
3840

41+
3942
/**
4043
* Set the message name.
4144
*/
@@ -46,9 +49,7 @@ public function name(?string $name): self
4649
return $this;
4750
}
4851

49-
/**
50-
* Set the message token.
51-
*/
52+
5253
public function token(?string $token): self
5354
{
5455
$this->token = $token;
@@ -66,22 +67,17 @@ public function topic(?string $topic): self
6667
return $this;
6768
}
6869

69-
/**
70-
* Set the message condition.
71-
*/
70+
7271
public function condition(?string $condition): self
7372
{
7473
$this->condition = $condition;
7574

7675
return $this;
7776
}
7877

79-
/**
80-
* Set the message data, or throw exception if data is not an array of strings.
81-
*/
8278
public function data(?array $data): self
8379
{
84-
if (! empty(array_filter($data, fn ($value) => ! is_string($value)))) {
80+
if (! empty(array_filter($data, fn($value) => ! is_string($value)))) {
8581
throw new InvalidArgumentException('Data values must be strings.');
8682
}
8783

@@ -90,36 +86,62 @@ public function data(?array $data): self
9086
return $this;
9187
}
9288

89+
90+
public function custom(?array $custom = []): self
91+
{
92+
$this->custom = $custom;
93+
94+
return $this;
95+
}
96+
97+
9398
/**
94-
* Set additional custom message data.
99+
* Set "android" specific custom options.
95100
*/
96-
public function custom(?array $custom): self
101+
public function android(array $options = []): self
97102
{
98-
$this->custom = $custom;
103+
104+
$this->custom([
105+
...$this->custom,
106+
'android' => $options,
107+
]);
99108

100109
return $this;
101110
}
102111

112+
113+
103114
/**
104-
* Set the message notification.
115+
* Set APNs-specific custom options for iOS.
116+
* Set APNs-specific custom options for iOS.
105117
*/
118+
public function ios(array $options = []): self
119+
{
120+
$this->custom([
121+
...$this->custom,
122+
'apns' => $options,
123+
'apns' => $options,
124+
]);
125+
126+
return $this;
127+
}
128+
129+
106130
public function notification(Notification $notification): self
107131
{
108132
$this->notification = $notification;
109133

110134
return $this;
111135
}
112136

113-
/**
114-
* Set the message Firebase Messaging client instance.
115-
*/
116137
public function usingClient(Messaging $client): self
117138
{
118139
$this->client = $client;
119140

120141
return $this;
121142
}
122143

144+
123145
public function toArray()
124146
{
125147
return array_filter([
@@ -133,6 +155,7 @@ public function toArray()
133155
]);
134156
}
135157

158+
136159
/**
137160
* @return mixed
138161
*/

tests/FcmMessageTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,43 @@ public function test_it_can_set_client()
109109

110110
$this->assertSame($client, $message->client);
111111
}
112+
113+
public function test_appends_android_options_into_custom()
114+
{
115+
$msg = FcmMessage::create()
116+
->notification(new Notification(title: 'T', body: 'B'))
117+
->android(['notification' => ['channel_id' => 'ops', 'sound' => 'default']]);
118+
119+
$payload = $msg->toArray();
120+
121+
$this->assertArrayHasKey('android', $payload);
122+
$this->assertArrayHasKey('notification', $payload['android']);
123+
$this->assertEquals('ops', $payload['android']['notification']['channel_id']);
124+
$this->assertEquals('default', $payload['android']['notification']['sound']);
125+
}
126+
127+
public function test_appends_ios_options_into_custom()
128+
{
129+
$msg = FcmMessage::create()
130+
->ios(['payload' => ['aps' => ['sound' => 'default']]]);
131+
132+
$payload = $msg->toArray();
133+
134+
$this->assertArrayHasKey('apns', $payload);
135+
$this->assertArrayHasKey('payload', $payload['apns']);
136+
$this->assertEquals('default', $payload['apns']['payload']['aps']['sound']);
137+
}
138+
139+
public function test_preserves_existing_custom_keys_when_using_helpers()
140+
{
141+
$msg = FcmMessage::create()
142+
->custom(['meta' => ['a' => 1]])
143+
->android(['notification' => ['color' => '#000']]);
144+
145+
$payload = $msg->toArray();
146+
147+
$this->assertArrayHasKey('meta', $payload);
148+
$this->assertEquals(1, $payload['meta']['a']);
149+
$this->assertEquals('#000', $payload['android']['notification']['color']);
150+
}
112151
}

0 commit comments

Comments
 (0)