Skip to content

Commit

Permalink
Add Push::setCollapseKey()
Browse files Browse the repository at this point in the history
Fixes #57
  • Loading branch information
Philipp91 committed Nov 27, 2022
1 parent 70216a3 commit 9e36055
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Push/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ abstract class Push implements Request
*/
protected $data = [];

/**
* @var string|null
*/
protected $collapseKey = null;

/**
* @param string|array $iidToken
* @return self
Expand Down Expand Up @@ -85,6 +90,18 @@ public function addData($name, $value): self
return $this;
}

/**
* @see https://firebase.google.com/docs/cloud-messaging/concept-options#collapsible_and_non-collapsible_messages
* @param string $collapseKey
*
* @return self
*/
public function setCollapseKey(string $collapseKey): self
{
$this->collapseKey = $collapseKey;
return $this;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -126,6 +143,10 @@ public function buildJsonBody(): array
$request['data'] = $this->data;
}

if (!empty($this->collapseKey)) {
$request['collapse_key'] = $this->collapseKey;
}

return $request;
}
}
20 changes: 20 additions & 0 deletions tests/PushTests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,26 @@ public function it_can_generate_a_data_push_with_data()
$this->assertEquals($expected, $data->buildJsonBody());
}

/** @test */
public function it_can_generate_a_data_push_with_collapse_key()
{
$data = new \Fcm\Push\Data();
$data
->addRecipient('device_1')
->addData('key', 'value')
->setCollapseKey('collapsekeyvalue');

$expected = [
'to' => 'device_1',
'data' => [
'key' => 'value',
],
'collapse_key' => 'collapsekeyvalue',
];

$this->assertEquals($expected, $data->buildJsonBody());
}

/** @test */
public function it_can_generate_a_quick_object_from_magic_method()
{
Expand Down
27 changes: 27 additions & 0 deletions tests/PushTests/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,33 @@ public function it_can_generate_a_notification_with_add_data_array_twice()
$this->assertEquals($expected, $notification->buildJsonBody());
}

/** @test */
public function it_can_generate_a_notification_with_collapse_key()
{
$notification = new \Fcm\Push\Notification();
$notification
->setTitle('Test title')
->setBody('A small body as an example')
->addRecipient('device_1')
->setCollapseKey('collapsekeyvalue');

$expected = [
'to' => 'device_1',
'notification' => [
'title' => 'Test title',
'body' => 'A small body as an example',
'sound' => '',
'icon' => '',
'color' => '',
'tag' => '',
'subtitle' => '',
],
'collapse_key' => 'collapsekeyvalue',
];

$this->assertEquals($expected, $notification->buildJsonBody());
$this->assertSame('https://fcm.googleapis.com/fcm/send', $notification->getUrl());
}

/** @test */
public function it_can_generate_a_quick_object_from_magic_method()
Expand Down

0 comments on commit 9e36055

Please sign in to comment.