diff --git a/src/Message.php b/src/Message.php index e39f13a..cc385ed 100644 --- a/src/Message.php +++ b/src/Message.php @@ -4,6 +4,7 @@ use paragraph1\phpFCM\Recipient\Recipient; use paragraph1\phpFCM\Recipient\Topic; use paragraph1\phpFCM\Recipient\Device; +use paragraph1\phpFCM\Recipient\GroupTopic; /** * @author palbertini @@ -28,6 +29,7 @@ class Message implements \JsonSerializable private $recipientType; private $timeToLive; private $delayWhileIdle; + private $mutableContent; /** * Represents the app's "Send-to-Sync" message. @@ -47,7 +49,7 @@ class Message implements \JsonSerializable */ public function addRecipient(Recipient $recipient) { - if (!$recipient instanceof Device && !$recipient instanceof Topic) { + if (!$recipient instanceof Device && !$recipient instanceof Topic && !$recipient instanceof GroupTopic) { throw new \UnexpectedValueException('currently phpFCM only supports topic and single device messages'); } @@ -123,6 +125,10 @@ public function setDelayWhileIdle($delayWhileIdle) return $this; } + public function setMutableContent() + { + $this->mutableContent = 1; + } /** * @see https://firebase.google.com/docs/cloud-messaging/concept-options#collapsible_and_non-collapsible_messages * @@ -166,6 +172,11 @@ public function jsonSerialize() if ($this->delayWhileIdle) { $jsonData['delay_while_idle'] = (bool)$this->delayWhileIdle; } + + if ($this->mutableContent) { + $jsonData['mutable_content'] = (bool)$this->mutableContent; + } + if ($this->contentAvailableFlag === TRUE) { $jsonData['content_available'] = TRUE; } @@ -187,6 +198,18 @@ function (Topic $topic) { return sprintf("'%s' in topics", $topic->getIdentifier } $jsonData['to'] = sprintf('/topics/%s', current($this->recipients)->getIdentifier()); break; + + case GroupTopic::class: + if (count($this->recipients) > 1) { + $topics = array_map( + function (GroupTopic $topic) { return sprintf("'%s' in topics", $topic->getIdentifier()); }, + $this->recipients + ); + $jsonData['condition'] = implode(' && ', $topics); + break; + } + $jsonData['to'] = sprintf('/topics/%s', current($this->recipients)->getIdentifier()); + break; default: if (count($this->recipients) === 1) { $jsonData['to'] = current($this->recipients)->getIdentifier(); @@ -199,4 +222,4 @@ function (Topic $topic) { return sprintf("'%s' in topics", $topic->getIdentifier } } } -} \ No newline at end of file +} diff --git a/src/Recipient/GroupTopic.php b/src/Recipient/GroupTopic.php new file mode 100644 index 0000000..c88934e --- /dev/null +++ b/src/Recipient/GroupTopic.php @@ -0,0 +1,17 @@ +name = $name; + } + + public function getIdentifier() + { + return $this->name; + } +} \ No newline at end of file diff --git a/tests/MessageTest.php b/tests/MessageTest.php index c52fa96..e955e85 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -133,6 +133,18 @@ public function testJsonEncodeHandlesDelayIdle() ); } + public function testJsonEncodeHandlesMutableContent() + { + $body = '{"to":"\/topics\/testing","data":{"foo":"bar"},"priority":"high","mutable_content":true}'; + $this->fixture->setData(['foo' => 'bar']) + ->setMutableContent(); + $this->fixture->addRecipient(new Topic('testing')); + $this->assertSame( + $body, + json_encode($this->fixture) + ); + } + public function testAddingNewAndUnknownRecipientTypesYieldsException() { $this->setExpectedException(\UnexpectedValueException::class);