diff --git a/src/Push/Push.php b/src/Push/Push.php index 604854b..bc3f538 100644 --- a/src/Push/Push.php +++ b/src/Push/Push.php @@ -22,6 +22,11 @@ abstract class Push implements Request */ protected $data = []; + /** + * @var string|null + */ + protected $collapseKey = null; + /** * @param string|array $iidToken * @return self @@ -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 */ @@ -126,6 +143,10 @@ public function buildJsonBody(): array $request['data'] = $this->data; } + if (!empty($this->collapseKey)) { + $request['collapse_key'] = $this->collapseKey; + } + return $request; } } diff --git a/tests/PushTests/DataTest.php b/tests/PushTests/DataTest.php index fda7595..8d63894 100644 --- a/tests/PushTests/DataTest.php +++ b/tests/PushTests/DataTest.php @@ -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() { diff --git a/tests/PushTests/NotificationTest.php b/tests/PushTests/NotificationTest.php index 32a9242..d00cff6 100644 --- a/tests/PushTests/NotificationTest.php +++ b/tests/PushTests/NotificationTest.php @@ -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()