Skip to content

Ability to pass meta data along with push notifications #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build
composer.phar
composer.lock
.phpunit.result.cache
/.idea
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class AccountApproved extends Notification
->iOS()
->badge(1)
->sound('success')
->meta(['foo' => 'bar'])
->body("Your {$notifiable->service} account was approved!");
}
}
Expand All @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions src/PusherMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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 @@ -266,6 +273,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;
}

/**
* Set the message link.
*
Expand Down Expand Up @@ -330,6 +351,10 @@ public function toiOS()
],
];

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

$this->formatMessage($message);

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

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

$this->formatMessage($message);

return $message;
Expand Down
18 changes: 18 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
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'));
}
}