Skip to content

Commit

Permalink
Merge fcb7db6 into 101db2d
Browse files Browse the repository at this point in the history
  • Loading branch information
Stalinko committed Jun 4, 2020
2 parents 101db2d + fcb7db6 commit f71a112
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ build
composer.phar
composer.lock
.phpunit.result.cache
/.idea
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -79,6 +79,7 @@ class AccountApproved extends Notification
->iOS()
->badge(1)
->sound('success')
->meta(['foo' => 'bar'])
->body("Your {$notifiable->service} account was approved!");
}
}
Expand All @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions src/PusherMessage.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -281,6 +302,10 @@ public function toiOS()
],
];

if (!empty($this->meta)) {
$message['apns']['data'] = $this->meta;
}

$this->formatMessage($message);

return $message;
Expand All @@ -304,6 +329,10 @@ public function toAndroid()
],
];

if (!empty($this->meta)) {
$message['fcm']['data'] = $this->meta;
}

$this->formatMessage($message);

return $message;
Expand Down
18 changes: 18 additions & 0 deletions tests/MessageTest.php
Expand Up @@ -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'));
}
}

0 comments on commit f71a112

Please sign in to comment.