diff --git a/.gitignore b/.gitignore index d07f69a..a70d7ea 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ build composer.phar composer.lock .phpunit.result.cache +/.idea diff --git a/README.md b/README.md index 23839c4..54e0e1e 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ class AccountApproved extends Notification ->iOS() ->badge(1) ->sound('success') + ->meta(['foo' => 'bar']) ->body("Your {$notifiable->service} account was approved!"); } } @@ -92,6 +93,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 d28370d..7dcf426 100644 --- a/src/PusherMessage.php +++ b/src/PusherMessage.php @@ -56,6 +56,13 @@ class PusherMessage */ protected $options = []; + /** + * Meta data that will be passed along with the message. + * + * @var array + */ + protected $meta = []; + /** * An extra message to the other platform. * @@ -236,6 +243,20 @@ public function badge($value) return $this; } + /** + * Set the metadata. + * + * @param array $meta + * + * @return $this + */ + public function meta(array $meta) + { + $this->meta = $meta; + + return $this; + } + /** * @param string $key * @param mixed $value @@ -281,6 +302,10 @@ public function toiOS() ], ]; + if (!empty($this->meta)) { + $message['apns']['data'] = $this->meta; + } + $this->formatMessage($message); return $message; @@ -304,6 +329,10 @@ public function toAndroid() ], ]; + if (!empty($this->meta)) { + $message['fcm']['data'] = $this->meta; + } + $this->formatMessage($message); return $message; diff --git a/tests/MessageTest.php b/tests/MessageTest.php index 6cde9f0..f4ad705 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -124,4 +124,22 @@ public function it_can_send_message_to_multiple_platforms() $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')); + } }