From 69d0d94fd04c0774f28611eba8e5a7d12626cf52 Mon Sep 17 00:00:00 2001 From: krsque <78272923+krsque@users.noreply.github.com> Date: Wed, 19 Jun 2024 16:40:57 +0400 Subject: [PATCH] Add group related notification ID (#15) Co-authored-by: Daniil Glushchenko --- .gitignore | 1 + .../Pushwoosh/Model/Notification/Android.php | 77 +++++++++++++------ .../Pushwoosh/Model/Notification/Huawei.php | 27 +++++++ .../Pushwoosh/Model/Notification/IOS.php | 49 +++++++++--- .../Model/Notification/AndroidTest.php | 19 +++-- .../Model/Notification/HuaweiTest.php | 9 +++ .../Pushwoosh/Model/Notification/IOSTest.php | 11 ++- .../Model/Notification/NotificationTest.php | 6 ++ 8 files changed, 161 insertions(+), 38 deletions(-) diff --git a/.gitignore b/.gitignore index 9b1548c..92a3b2f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /phpdocumentor/ /build/ composer.lock +.idea/ diff --git a/src/main/php/Gomoob/Pushwoosh/Model/Notification/Android.php b/src/main/php/Gomoob/Pushwoosh/Model/Notification/Android.php index 2919d4d..1e1a31c 100644 --- a/src/main/php/Gomoob/Pushwoosh/Model/Notification/Android.php +++ b/src/main/php/Gomoob/Pushwoosh/Model/Notification/Android.php @@ -22,7 +22,7 @@ class Android implements \JsonSerializable * @var int */ private $badges; - + private $banner; private $customIcon; @@ -33,24 +33,33 @@ class Android implements \JsonSerializable * @var int */ private $gcmTtl; + + /** + * Identifier to group related notifications. + * Messages with the same thread ID will be grouped in the Notification Center. + * + * @var string|null + */ + private $groupId; + private $header; - + /** * The icon background color on Lollipop, #RRGGBB, #AARRGGBB, "red", "black", "yellow", etc. * * @var string */ private $ibc; - + private $icon; - + /** * The LED hex color, device will do its best approximation. * * @var string */ private $led; - + /** * The priority of the push in the Android push drawer, valid values are -2, -1, 0, 1 and 2. * @@ -60,7 +69,7 @@ class Android implements \JsonSerializable private $rootParams; private $sound; - + /** * A boolean used to force vibration for high-priority pushes. * @@ -113,12 +122,20 @@ public function getGcmTtl() } + /** + * @return string|null + */ + public function getGroupId() + { + return $this->groupId; + } + public function getHeader() { return $this->header; } - + /** * Gets the icon background color on Lollipop, #RRGGBB, #AARRGGBB, "red", "black", "yellow", etc. * @@ -133,7 +150,7 @@ public function getIcon() { return $this->icon; } - + /** * Gets the LED hex color, device will do its best approximation. * @@ -143,7 +160,7 @@ public function getLed() { return $this->led; } - + /** * Gets priority of the push in the Android push drawer, valid values are -2, -1, 0, 1 and 2. * @@ -163,7 +180,7 @@ public function getSound() { return $this->sound; } - + /** * Gets the boolean used to force vibration for high-priority pushes. * @@ -173,14 +190,14 @@ public function isVibration() { return $this->vibration; } - + /** * {@inheritdoc} */ public function jsonSerialize() { $json = []; - + isset($this->badges) ? $json['android_badges'] = $this->badges : false; isset($this->banner) ? $json['android_banner'] = $this->banner : false; isset($this->customIcon) ? $json['android_custom_icon'] = $this->customIcon : false; @@ -193,9 +210,13 @@ public function jsonSerialize() isset($this->rootParams) ? $json['android_root_params'] = $this->rootParams : false; isset($this->sound) ? $json['android_sound'] = $this->sound : false; isset($this->vibration) ? $json['android_vibration'] = ($this->vibration ? 1 : 0) : false; - + + if ($this->groupId !== null) { + $json['android_group_id'] = $this->groupId; + } + return $json; - + } /** @@ -208,7 +229,7 @@ public function jsonSerialize() public function setBadges($badges) { $this->badges = $badges; - + return $this; } @@ -242,6 +263,16 @@ public function setGcmTtl($gcmTtl) return $this; } + /** + * @param string|null $groupId + */ + public function setGroupId($groupId) + { + $this->groupId = $groupId; + + return $this; + } + public function setHeader($header) { $this->header = $header; @@ -249,7 +280,7 @@ public function setHeader($header) return $this; } - + /** * Sets the icon background color on Lollipop, #RRGGBB, #AARRGGBB, "red", "black", "yellow", etc. * @@ -260,7 +291,7 @@ public function setHeader($header) public function setIbc($ibc) { $this->ibc = $ibc; - + return $this; } @@ -270,7 +301,7 @@ public function setIcon($icon) return $this; } - + /** * Sets the LED hex color, device will do its best approximation. * @@ -281,10 +312,10 @@ public function setIcon($icon) public function setLed($led) { $this->led = $led; - + return $this; } - + /** * Sets the priority of the push in the Android push drawer, valid values are -2, -1, 0, 1 and 2. * @@ -295,7 +326,7 @@ public function setLed($led) public function setPriority($priority) { $this->priority = $priority; - + return $this; } @@ -312,7 +343,7 @@ public function setSound($sound) return $this; } - + /** * Sets the boolean used to force vibration for high-priority pushes. * @@ -323,7 +354,7 @@ public function setSound($sound) public function setVibration($vibration) { $this->vibration = $vibration; - + return $this; } } diff --git a/src/main/php/Gomoob/Pushwoosh/Model/Notification/Huawei.php b/src/main/php/Gomoob/Pushwoosh/Model/Notification/Huawei.php index 1628a13..193927d 100644 --- a/src/main/php/Gomoob/Pushwoosh/Model/Notification/Huawei.php +++ b/src/main/php/Gomoob/Pushwoosh/Model/Notification/Huawei.php @@ -24,6 +24,11 @@ final class Huawei implements \JsonSerializable */ private $gcmTtl; + /** + * @var string|null + */ + private $groupId; + /** * @var string|null */ @@ -89,6 +94,14 @@ public function getGcmTtl() return $this->gcmTtl; } + /** + * @return string|null + */ + public function getGroupId() + { + return $this->groupId; + } + public function getHeader() { return $this->header; @@ -149,6 +162,10 @@ public function jsonSerialize() $json['huawei_android_gcm_ttl'] = $this->gcmTtl; } + if ($this->groupId !== null) { + $json['huawei_android_group_id'] = $this->groupId; + } + if ($this->header !== null) { $json['huawei_android_header'] = $this->header; } @@ -212,6 +229,16 @@ public function setGcmTtl($gcmTtl) return $this; } + /** + * @param string|null $groupId + */ + public function setGroupId($groupId) + { + $this->groupId = $groupId; + + return $this; + } + public function setHeader($header) { $this->header = $header; diff --git a/src/main/php/Gomoob/Pushwoosh/Model/Notification/IOS.php b/src/main/php/Gomoob/Pushwoosh/Model/Notification/IOS.php index 5643814..b08d684 100644 --- a/src/main/php/Gomoob/Pushwoosh/Model/Notification/IOS.php +++ b/src/main/php/Gomoob/Pushwoosh/Model/Notification/IOS.php @@ -23,16 +23,25 @@ class IOS implements \JsonSerializable private $apnsTrimContent; private $badges; - + /** * The iOS 8 category ID from Pushwoosh. * * @var int */ private $categoryId; - + private $rootParams; private $sound; + + /** + * Identifier to group related notifications. + * Messages with the same thread ID will be grouped on the lock screen and in the Notification Center. + * + * @var string|null + */ + private $threadId; + private $ttl; private $trimContent; @@ -50,7 +59,7 @@ public function getBadges() { return $this->badges; } - + /** * Gets the iOS 8 category ID from Pushwoosh. * @@ -72,6 +81,14 @@ public function getSound() } + /** + * @return string|null + */ + public function getThreadId() + { + return $this->threadId; + } + public function getTtl() { return $this->ttl; @@ -93,14 +110,14 @@ public function isTrimContent() return $this->trimContent; } - + /** * {@inheritdoc} */ public function jsonSerialize() { $json = []; - + isset($this->apnsTrimContent) ? $json['apns_trim_content'] = intval($this->apnsTrimContent) : false; isset($this->badges) ? $json['ios_badges'] = $this->badges : false; isset($this->categoryId) ? $json['ios_category_id'] = $this->categoryId : false; @@ -108,9 +125,13 @@ public function jsonSerialize() isset($this->sound) ? $json['ios_sound'] = $this->sound : false; isset($this->ttl) ? $json['ios_ttl'] = $this->ttl : false; isset($this->trimContent) ? $json['ios_trim_content'] = intval($this->trimContent) : false; - + + if ($this->threadId !== null) { + $json['ios_thread_id'] = $this->threadId; + } + return $json; - + } /** @@ -135,7 +156,7 @@ public function setBadges($badges) return $this; } - + /** * Sets the iOS 8 category ID from Pushwoosh. * @@ -146,7 +167,7 @@ public function setBadges($badges) public function setCategoryId($categoryId) { $this->categoryId = $categoryId; - + return $this; } @@ -165,6 +186,16 @@ public function setSound($sound) return $this; } + /** + * @param string|null $threadId + */ + public function setThreadId($threadId) + { + $this->threadId = $threadId; + + return $this; + } + public function setTtl($ttl) { $this->ttl = $ttl; diff --git a/src/test/php/Gomoob/Pushwoosh/Model/Notification/AndroidTest.php b/src/test/php/Gomoob/Pushwoosh/Model/Notification/AndroidTest.php index a5ea5f7..d97ec31 100644 --- a/src/test/php/Gomoob/Pushwoosh/Model/Notification/AndroidTest.php +++ b/src/test/php/Gomoob/Pushwoosh/Model/Notification/AndroidTest.php @@ -25,7 +25,7 @@ public function testCreate() { $this->assertNotNull(Android::create()); } - + /** * Test method for the #getBadges() and setBadges($badges) functions. */ @@ -66,6 +66,13 @@ public function testGetSetGcmTtl() $this->assertSame(3600, $android->getGcmTtl()); } + public function testGetSetGroupId() + { + $android = new Android(); + $this->assertSame($android, $android->setGroupId('test_group')); + $this->assertSame('test_group', $android->getGroupId()); + } + /** * Test method for the #getHeader() and #setHeader($header) functions. */ @@ -85,7 +92,7 @@ public function testGetSetIbc() $this->assertSame($android, $android->setIbc('#AA9966')); $this->assertSame('#AA9966', $android->getIbc()); } - + /** * Test method for the #getIcon() and #setIcon($icon) functions. */ @@ -105,7 +112,7 @@ public function testGetSetLed() $this->assertSame($android, $android->setLed('#4455cc')); $this->assertSame('#4455cc', $android->getLed()); } - + /** * Test method for the #getPriority() and #setPriority($priority) functions. */ @@ -115,7 +122,7 @@ public function testGetSetPriority() $this->assertSame($android, $android->setPriority(-1)); $this->assertSame(-1, $android->getPriority()); } - + /** * Test method for the #getRootParams() and #setRootParams($rootParams) functions. */ @@ -145,7 +152,7 @@ public function testIsSetVibration() $this->assertSame($android, $android->setVibration(true)); $this->assertTrue($android->isVibration()); } - + /** * Test method for the #jsonSerialize() function. */ @@ -156,6 +163,7 @@ public function testJsonSerialize() ->setBadges(5) ->setCustomIcon('http://example.com/image.png') ->setGcmTtl(3600) + ->setGroupId('test_group') ->setHeader('Header') ->setIbc('#AA9966') ->setIcon('icon') @@ -171,6 +179,7 @@ public function testJsonSerialize() $this->assertSame(5, $array['android_badges']); $this->assertSame('http://example.com/image.png', $array['android_custom_icon']); $this->assertSame(3600, $array['android_gcm_ttl']); + $this->assertSame('test_group', $array['android_group_id']); $this->assertSame('Header', $array['android_header']); $this->assertSame('#AA9966', $array['android_ibc']); $this->assertSame('icon', $array['android_icon']); diff --git a/src/test/php/Gomoob/Pushwoosh/Model/Notification/HuaweiTest.php b/src/test/php/Gomoob/Pushwoosh/Model/Notification/HuaweiTest.php index dac94ef..217c616 100644 --- a/src/test/php/Gomoob/Pushwoosh/Model/Notification/HuaweiTest.php +++ b/src/test/php/Gomoob/Pushwoosh/Model/Notification/HuaweiTest.php @@ -39,6 +39,13 @@ public function testGetSetGcmTtl() $this->assertSame(3600, $huawei->getGcmTtl()); } + public function testGetSetGroupId() + { + $huawei = new Huawei(); + $this->assertSame($huawei, $huawei->setGroupId('test_group')); + $this->assertSame('test_group', $huawei->getGroupId()); + } + public function testGetSetHeader() { $huawei = new Huawei(); @@ -102,6 +109,7 @@ public function testJsonSerialize() ->setBadges(5) ->setCustomIcon('http://example.com/image.png') ->setGcmTtl(3600) + ->setGroupId('test_group') ->setHeader('Header') ->setIbc('#AA9966') ->setIcon('icon') @@ -117,6 +125,7 @@ public function testJsonSerialize() $this->assertSame(5, $array['huawei_android_badges']); $this->assertSame('http://example.com/image.png', $array['huawei_android_custom_icon']); $this->assertSame(3600, $array['huawei_android_gcm_ttl']); + $this->assertSame('test_group', $array['huawei_android_group_id']); $this->assertSame('Header', $array['huawei_android_header']); $this->assertSame('#AA9966', $array['huawei_android_ibc']); $this->assertSame('icon', $array['huawei_android_icon']); diff --git a/src/test/php/Gomoob/Pushwoosh/Model/Notification/IOSTest.php b/src/test/php/Gomoob/Pushwoosh/Model/Notification/IOSTest.php index 91a7a43..9390a2c 100644 --- a/src/test/php/Gomoob/Pushwoosh/Model/Notification/IOSTest.php +++ b/src/test/php/Gomoob/Pushwoosh/Model/Notification/IOSTest.php @@ -35,7 +35,7 @@ public function testGetSetBadges() $this->assertSame($iOS, $iOS->setBadges(5)); $this->assertSame(5, $iOS->getBadges()); } - + /** * Test method for the #getCategoryId() and #setCategoryId($categoryId) functions. */ @@ -66,6 +66,13 @@ public function testGetSetSound() $this->assertSame('sound file.wav', $iOS->getSound()); } + public function testGetSetThreadId() + { + $iOS = new IOS(); + $this->assertSame($iOS, $iOS->setThreadId('test_thread')); + $this->assertSame('test_thread', $iOS->getThreadId()); + } + /** * Test method for the #getTtl() and #setTtl($ttl) functions. */ @@ -108,6 +115,7 @@ public function testJsonSerialize() ->setCategoryId('1') ->setRootParams(['aps' => ['content-available' => '1']]) ->setSound('sound file.wav') + ->setThreadId('test_thread') ->setTtl(3600) ->setTrimContent(true) ->jsonSerialize(); @@ -118,6 +126,7 @@ public function testJsonSerialize() $this->assertSame('1', $array['ios_category_id']); $this->assertSame(['aps' => ['content-available' => '1']], $array['ios_root_params']); $this->assertSame('sound file.wav', $array['ios_sound']); + $this->assertSame('test_thread', $array['ios_thread_id']); $this->assertSame(3600, $array['ios_ttl']); $this->assertSame(1, $array['ios_trim_content']); } diff --git a/src/test/php/Gomoob/Pushwoosh/Model/Notification/NotificationTest.php b/src/test/php/Gomoob/Pushwoosh/Model/Notification/NotificationTest.php index 0e62d35..1d4a03f 100644 --- a/src/test/php/Gomoob/Pushwoosh/Model/Notification/NotificationTest.php +++ b/src/test/php/Gomoob/Pushwoosh/Model/Notification/NotificationTest.php @@ -537,6 +537,7 @@ public function testJsonSerialize() ->setBanner('http://example.com/banner.png') ->setCustomIcon('http://example.com/image.png') ->setGcmTtl(3600) + ->setGroupId('test_group') ->setHeader('Header') ->setIbc('#AA9966') ->setIcon('icon') @@ -552,6 +553,7 @@ public function testJsonSerialize() ->setBanner('http://example.com/banner.png') ->setCustomIcon('http://example.com/image.png') ->setGcmTtl(3600) + ->setGroupId('test_group') ->setHeader('Header') ->setIbc('#AA9966') ->setIcon('icon') @@ -579,6 +581,7 @@ public function testJsonSerialize() ->setCategoryId('1') ->setRootParams(['aps' => ['content-available' => '1']]) ->setSound('sound file.wav') + ->setThreadId('test_thread') ->setTtl(3600) ->setTrimContent(true) ) @@ -681,6 +684,7 @@ public function testJsonSerialize() $this->assertSame('http://example.com/banner.png', $array['android_banner']); $this->assertSame('http://example.com/image.png', $array['android_custom_icon']); $this->assertSame(3600, $array['android_gcm_ttl']); + $this->assertSame('test_group', $array['android_group_id']); $this->assertSame('Header', $array['android_header']); $this->assertSame('#AA9966', $array['android_ibc']); $this->assertSame('icon', $array['android_icon']); @@ -695,6 +699,7 @@ public function testJsonSerialize() $this->assertSame('http://example.com/banner.png', $array['huawei_android_banner']); $this->assertSame('http://example.com/image.png', $array['huawei_android_custom_icon']); $this->assertSame(3600, $array['huawei_android_gcm_ttl']); + $this->assertSame('test_group', $array['huawei_android_group_id']); $this->assertSame('Header', $array['huawei_android_header']); $this->assertSame('#AA9966', $array['huawei_android_ibc']); $this->assertSame('icon', $array['huawei_android_icon']); @@ -719,6 +724,7 @@ public function testJsonSerialize() $this->assertSame('1', $array['ios_category_id']); $this->assertSame(['aps' => ['content-available' => '1']], $array['ios_root_params']); $this->assertSame('sound file.wav', $array['ios_sound']); + $this->assertSame('test_thread', $array['ios_thread_id']); $this->assertSame(3600, $array['ios_ttl']); $this->assertSame(1, $array['ios_trim_content']);