From 799ae5941048ad58038b6270ed18f011651639c9 Mon Sep 17 00:00:00 2001 From: Michael Ryvlin Date: Mon, 9 Jan 2017 15:24:16 +0100 Subject: [PATCH 1/2] added "priority" flag of a message --- library/CodeMonkeysRu/GCM/Message.php | 28 +++++++++++++++++++++++++++ library/CodeMonkeysRu/GCM/Sender.php | 1 + 2 files changed, 29 insertions(+) diff --git a/library/CodeMonkeysRu/GCM/Message.php b/library/CodeMonkeysRu/GCM/Message.php index f116005..41822ce 100644 --- a/library/CodeMonkeysRu/GCM/Message.php +++ b/library/CodeMonkeysRu/GCM/Message.php @@ -96,6 +96,21 @@ class Message */ private $contentAvailable = true; + /** + * Sets the priority of the message. Valid values are "normal" and "high." On iOS, these + * correspond to APNs priority 5 and 10. By default, messages are sent with normal + * priority. Normal priority optimizes the client app's battery consumption, and should + * be used unless immediate delivery is required. For messages with normal priority, the + * app may receive the message with unspecified delay. + * When a message is sent with high priority, it is sent immediately, and the app can wake + * a sleeping device and open a network connection to your server. + * + * Optional. + * + * @var string + */ + private $priority = 'normal'; + /** * Allows developers to test their request without actually sending a message. * @@ -222,4 +237,17 @@ public function setContentAvailable($contentAvailable) $this->contentAvailable = $contentAvailable; return $this; } + + public function getPriority() + { + return $this->priority; + } + + public function setPriority($priority) + { + if (in_array($priority, array('high', 'normal'), true)) { + $this->priority = $priority; + } + return $this; + } } diff --git a/library/CodeMonkeysRu/GCM/Sender.php b/library/CodeMonkeysRu/GCM/Sender.php index ac3e149..19a21f1 100644 --- a/library/CodeMonkeysRu/GCM/Sender.php +++ b/library/CodeMonkeysRu/GCM/Sender.php @@ -156,6 +156,7 @@ private function formMessageData(Message $message) 'restricted_package_name' => 'getRestrictedPackageName', 'dry_run' => 'getDryRun', 'content_available' => 'getContentAvailable', + 'priority' => 'getPriority', ); foreach ($dataFields as $fieldName => $getter) { From 485a5d42baf6f18a25d8a76270c247c9d407631d Mon Sep 17 00:00:00 2001 From: Michael Ryvlin Date: Wed, 11 Jan 2017 10:09:25 +0100 Subject: [PATCH 2/2] minor refactoring --- library/CodeMonkeysRu/GCM/Message.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/library/CodeMonkeysRu/GCM/Message.php b/library/CodeMonkeysRu/GCM/Message.php index 41822ce..b929b5b 100644 --- a/library/CodeMonkeysRu/GCM/Message.php +++ b/library/CodeMonkeysRu/GCM/Message.php @@ -8,6 +8,8 @@ */ class Message { + const PRIORITY_NORMAL = 'normal'; + const PRIORITY_HIGH = 'high'; /** * A string array with the list of devices (registration IDs) receiving the message. @@ -109,7 +111,7 @@ class Message * * @var string */ - private $priority = 'normal'; + private $priority = self::PRIORITY_NORMAL; /** * Allows developers to test their request without actually sending a message. @@ -245,9 +247,15 @@ public function getPriority() public function setPriority($priority) { - if (in_array($priority, array('high', 'normal'), true)) { - $this->priority = $priority; + $allowedPriorities = array(self::PRIORITY_HIGH, self::PRIORITY_NORMAL); + + if (!in_array($priority, $allowedPriorities, true)) { + throw new \InvalidArgumentException( + 'Invalid priority "' . $priority . '", allowed are only these: ' . implode(', ', $allowedPriorities) + ); } + + $this->priority = $priority; return $this; } }