From 5971bd5de13cf923cd7f8ad8efb7120377e4eae7 Mon Sep 17 00:00:00 2001 From: Philipp Keck Date: Sun, 27 Nov 2022 20:32:35 +0100 Subject: [PATCH] Add Push::setPriority() Fixes #48 --- src/Push/Push.php | 24 ++++++++++++++++++++++++ tests/PushTests/DataTest.php | 20 ++++++++++++++++++++ tests/PushTests/NotificationTest.php | 28 ++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/src/Push/Push.php b/src/Push/Push.php index bc3f538..0a936b3 100644 --- a/src/Push/Push.php +++ b/src/Push/Push.php @@ -7,6 +7,9 @@ abstract class Push implements Request { + const PRIORITY_HIGH = 'high'; + const PRIORITY_NORMAL = 'normal'; + /** * @var array */ @@ -22,6 +25,11 @@ abstract class Push implements Request */ protected $data = []; + /** + * @var string|null + */ + protected $priority = null; + /** * @var string|null */ @@ -90,6 +98,18 @@ public function addData($name, $value): self return $this; } + /** + * @see https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message + * @param string $priority + * + * @return self + */ + public function setPriority(string $priority): self + { + $this->priority = $priority; + return $this; + } + /** * @see https://firebase.google.com/docs/cloud-messaging/concept-options#collapsible_and_non-collapsible_messages * @param string $collapseKey @@ -143,6 +163,10 @@ public function buildJsonBody(): array $request['data'] = $this->data; } + if (!empty($this->priority)) { + $request['priority'] = $this->priority; + } + if (!empty($this->collapseKey)) { $request['collapse_key'] = $this->collapseKey; } diff --git a/tests/PushTests/DataTest.php b/tests/PushTests/DataTest.php index 8d63894..709fb1b 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_priority() + { + $data = new \Fcm\Push\Data(); + $data + ->addRecipient('device_1') + ->addData('key', 'value') + ->setPriority(\Fcm\Push\Data::PRIORITY_HIGH); + + $expected = [ + 'to' => 'device_1', + 'data' => [ + 'key' => 'value', + ], + 'priority' => 'high', + ]; + + $this->assertEquals($expected, $data->buildJsonBody()); + } + /** @test */ public function it_can_generate_a_data_push_with_collapse_key() { diff --git a/tests/PushTests/NotificationTest.php b/tests/PushTests/NotificationTest.php index d00cff6..e10927f 100644 --- a/tests/PushTests/NotificationTest.php +++ b/tests/PushTests/NotificationTest.php @@ -284,6 +284,34 @@ 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_priority() + { + $notification = new \Fcm\Push\Notification(); + $notification + ->setTitle('Test title') + ->setBody('A small body as an example') + ->addRecipient('device_1') + ->setPriority(\Fcm\Push\Notification::PRIORITY_NORMAL); + + $expected = [ + 'to' => 'device_1', + 'notification' => [ + 'title' => 'Test title', + 'body' => 'A small body as an example', + 'sound' => '', + 'icon' => '', + 'color' => '', + 'tag' => '', + 'subtitle' => '', + ], + 'priority' => 'normal', + ]; + + $this->assertEquals($expected, $notification->buildJsonBody()); + $this->assertSame('https://fcm.googleapis.com/fcm/send', $notification->getUrl()); + } + /** @test */ public function it_can_generate_a_notification_with_collapse_key() {