Skip to content

Commit fcb7db6

Browse files
committed
new method for adding meta-data to push messages
1 parent 101db2d commit fcb7db6

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ build
33
composer.phar
44
composer.lock
55
.phpunit.result.cache
6+
/.idea

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class AccountApproved extends Notification
7979
->iOS()
8080
->badge(1)
8181
->sound('success')
82+
->meta(['foo' => 'bar'])
8283
->body("Your {$notifiable->service} account was approved!");
8384
}
8485
}
@@ -92,6 +93,7 @@ class AccountApproved extends Notification
9293
- `title('')`: Accepts a string value for the title.
9394
- `body('')`: Accepts a string value for the body.
9495
- `sound('')`: Accepts a string value for the notification sound file. Notice that if you leave blank the default sound value will be `default`.
96+
- `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)
9597
- `icon('')`: Accepts a string value for the icon file. (Android Only)
9698
- `badge(1)`: Accepts an integer value for the badge. (iOS Only)
9799
- `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
@@ -56,6 +56,13 @@ class PusherMessage
5656
*/
5757
protected $options = [];
5858

59+
/**
60+
* Meta data that will be passed along with the message.
61+
*
62+
* @var array
63+
*/
64+
protected $meta = [];
65+
5966
/**
6067
* An extra message to the other platform.
6168
*
@@ -236,6 +243,20 @@ public function badge($value)
236243
return $this;
237244
}
238245

246+
/**
247+
* Set the metadata.
248+
*
249+
* @param array $meta
250+
*
251+
* @return $this
252+
*/
253+
public function meta(array $meta)
254+
{
255+
$this->meta = $meta;
256+
257+
return $this;
258+
}
259+
239260
/**
240261
* @param string $key
241262
* @param mixed $value
@@ -281,6 +302,10 @@ public function toiOS()
281302
],
282303
];
283304

305+
if (!empty($this->meta)) {
306+
$message['apns']['data'] = $this->meta;
307+
}
308+
284309
$this->formatMessage($message);
285310

286311
return $message;
@@ -304,6 +329,10 @@ public function toAndroid()
304329
],
305330
];
306331

332+
if (!empty($this->meta)) {
333+
$message['fcm']['data'] = $this->meta;
334+
}
335+
307336
$this->formatMessage($message);
308337

309338
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()
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)