From cc468db4ec9f2c92206fc49e4e0e2f000c8dbb3f Mon Sep 17 00:00:00 2001 From: Dan Khan Date: Wed, 22 May 2024 13:56:26 +1200 Subject: [PATCH] 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 #65 https://github.com/laravel-notification-channels/pusher-push-notifications/pull/65 --- README.md | 2 ++ src/PusherMessage.php | 29 +++++++++++++++++++++++++++++ tests/MessageTest.php | 18 ++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/README.md b/README.md index f230d30..f4f4f28 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ class AccountApproved extends Notification ->iOS() ->badge(1) ->sound('success') + ->meta(['foo' => 'bar']) ->body("Your {$notifiable->service} account was approved!"); } } @@ -99,6 +100,7 @@ class AccountApproved extends Notification - `title('')`: Accepts a string value for the title. - `body('')`: Accepts a string value for the body. - `sound('')`: Accepts a string value for the notification sound file. Notice that if you leave blank the default sound value will be `default`. +- `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) - `icon('')`: Accepts a string value for the icon file. (Android Only) - `badge(1)`: Accepts an integer value for the badge. (iOS Only) - `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. diff --git a/src/PusherMessage.php b/src/PusherMessage.php index f9caef9..5fff2ae 100644 --- a/src/PusherMessage.php +++ b/src/PusherMessage.php @@ -42,6 +42,13 @@ class PusherMessage */ protected array $options = []; + /** + * Meta data that will be passed along with the message. + * + * @var array + */ + protected array $meta = []; + /** * An extra message to the other platform. * @@ -247,6 +254,20 @@ public function badge(int $value): self return $this; } + /** + * Set the metadata. + * + * @param array $meta + * + * @return $this + */ + public function meta(array $meta) + { + $this->meta = $meta; + + return $this; + } + /** * Set the message link. * @@ -306,6 +327,10 @@ public function toiOS(): array ], ]; + if ($this->meta && count($this->meta)) { + $message['apns']['data'] = $this->meta; + } + $this->formatMessage($message); return $message; @@ -329,6 +354,10 @@ public function toAndroid(): array ], ]; + if ($this->meta && count($this->meta)) { + $message['fcm']['data'] = $this->meta; + } + $this->formatMessage($message); return $message; diff --git a/tests/MessageTest.php b/tests/MessageTest.php index fe223d0..6fcc51d 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -124,4 +124,22 @@ public function it_can_send_message_to_multiple_platforms(): void $this->assertTrue(Arr::has($this->message->toArray(), 'apns')); $this->assertTrue(Arr::has($this->message->toArray(), 'fcm')); } + + /** @test */ + public function it_has_no_meta_by_default() + { + $this->message; + $this->assertFalse(Arr::has($this->message->toArray(), 'apns.data')); + $this->assertFalse(Arr::has($this->message->toArray(), 'apns.data')); + } + + /** @test */ + public function it_can_add_meta() + { + $this->message->meta(['foo' => 'bar']); + $this->assertEquals(['foo' => 'bar'], Arr::get($this->message->toArray(), 'apns.data')); + + $this->message->android(); + $this->assertEquals(['foo' => 'bar'], Arr::get($this->message->toArray(), 'fcm.data')); + } }