Skip to content

Commit cc468db

Browse files
Add support for additional metadata
- Update Pusher Beams message to pass meta data along with message parameters: @see https://pusher.com/docs/beams/guides/publishing-to-multiple-devices#adding-metadata-to-a-notification - From PR laravel-notification-channels#65 laravel-notification-channels#65
1 parent 9ef230c commit cc468db

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class AccountApproved extends Notification
8484
->iOS()
8585
->badge(1)
8686
->sound('success')
87+
->meta(['foo' => 'bar'])
8788
->body("Your {$notifiable->service} account was approved!");
8889
}
8990
}
@@ -99,6 +100,7 @@ class AccountApproved extends Notification
99100
- `title('')`: Accepts a string value for the title.
100101
- `body('')`: Accepts a string value for the body.
101102
- `sound('')`: Accepts a string value for the notification sound file. Notice that if you leave blank the default sound value will be `default`.
103+
- `meta([...])`: Accepts an array of custom data to be sent along with the push message. Works for both platforms. See more at [Pusher Beams - Adding metadata to a notification](https://pusher.com/docs/beams/guides/publishing-to-multiple-devices)
102104
- `icon('')`: Accepts a string value for the icon file. (Android Only)
103105
- `badge(1)`: Accepts an integer value for the badge. (iOS Only)
104106
- `setOption($key, $value)`: Allows you to set any value in the message payload. See the [request body section of the Pusher Beam docs](https://pusher.com/docs/beams/reference/publish-api#request-body) for more information.

src/PusherMessage.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class PusherMessage
4242
*/
4343
protected array $options = [];
4444

45+
/**
46+
* Meta data that will be passed along with the message.
47+
*
48+
* @var array
49+
*/
50+
protected array $meta = [];
51+
4552
/**
4653
* An extra message to the other platform.
4754
*
@@ -247,6 +254,20 @@ public function badge(int $value): self
247254
return $this;
248255
}
249256

257+
/**
258+
* Set the metadata.
259+
*
260+
* @param array $meta
261+
*
262+
* @return $this
263+
*/
264+
public function meta(array $meta)
265+
{
266+
$this->meta = $meta;
267+
268+
return $this;
269+
}
270+
250271
/**
251272
* Set the message link.
252273
*
@@ -306,6 +327,10 @@ public function toiOS(): array
306327
],
307328
];
308329

330+
if ($this->meta && count($this->meta)) {
331+
$message['apns']['data'] = $this->meta;
332+
}
333+
309334
$this->formatMessage($message);
310335

311336
return $message;
@@ -329,6 +354,10 @@ public function toAndroid(): array
329354
],
330355
];
331356

357+
if ($this->meta && count($this->meta)) {
358+
$message['fcm']['data'] = $this->meta;
359+
}
360+
332361
$this->formatMessage($message);
333362

334363
return $message;

tests/MessageTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,22 @@ public function it_can_send_message_to_multiple_platforms(): void
124124
$this->assertTrue(Arr::has($this->message->toArray(), 'apns'));
125125
$this->assertTrue(Arr::has($this->message->toArray(), 'fcm'));
126126
}
127+
128+
/** @test */
129+
public function it_has_no_meta_by_default()
130+
{
131+
$this->message;
132+
$this->assertFalse(Arr::has($this->message->toArray(), 'apns.data'));
133+
$this->assertFalse(Arr::has($this->message->toArray(), 'apns.data'));
134+
}
135+
136+
/** @test */
137+
public function it_can_add_meta()
138+
{
139+
$this->message->meta(['foo' => 'bar']);
140+
$this->assertEquals(['foo' => 'bar'], Arr::get($this->message->toArray(), 'apns.data'));
141+
142+
$this->message->android();
143+
$this->assertEquals(['foo' => 'bar'], Arr::get($this->message->toArray(), 'fcm.data'));
144+
}
127145
}

0 commit comments

Comments
 (0)