From f6a636b4f4bb12019a90ad47dbd958763c1a9fc0 Mon Sep 17 00:00:00 2001 From: patrick Date: Thu, 2 Jun 2016 08:52:09 +0200 Subject: [PATCH] enabled messaging to multiple topics via conditions --- README.md | 3 --- src/Message.php | 20 +++++++++++--------- tests/MessageTest.php | 40 +++++++++++++++++++++------------------- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 8a84749..56d68ee 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,6 @@ var_dump($response->getStatusCode()); ``` #Send to topic -Currently sending to topics only supports a single topic as recipient. Mutliple topic as outlined -in the google docs don't seem to work, yet. - also see https://firebase.google.com/docs/cloud-messaging/topic-messaging ```php use paragraph1\phpFCM\Client; diff --git a/src/Message.php b/src/Message.php index 50c416a..627468e 100644 --- a/src/Message.php +++ b/src/Message.php @@ -94,7 +94,7 @@ public function jsonSerialize() throw new \UnexpectedValueException('message must have at least one recipient'); } - $jsonData['to'] = $this->createTo(); + $this->createTo($jsonData); if ($this->collapseKey) { $jsonData['collapse_key'] = $this->collapseKey; } @@ -111,26 +111,28 @@ public function jsonSerialize() return $jsonData; } - private function createTo() + private function createTo(array &$jsonData) { switch ($this->recipientType) { case Topic::class: if (count($this->recipients) > 1) { - throw new \UnexpectedValueException( - 'currently fcm messages to target multiple topics dont work, but its obviously planned: '. - 'https://firebase.google.com/docs/cloud-messaging/topic-messaging#sending_topic_messages_from_the_server' + $topics = array_map( + function (Topic $topic) { return sprintf("'%s' in topics", $topic->getName()); }, + $this->recipients ); + $jsonData['condition'] = implode(' || ', $topics); + break; } - return sprintf('/topics/%s', current($this->recipients)->getName()); + $jsonData['to'] = sprintf('/topics/%s', current($this->recipients)->getName()); break; case Device::class: if (count($this->recipients) == 1) { - return current($this->recipients)->getToken(); + $jsonData['to'] = current($this->recipients)->getToken(); + break; } - break; default: - throw new \UnexpectedValueException('currently phpFCM only supports single topic and single device messages'); + throw new \UnexpectedValueException('currently phpFCM only supports topic and single device messages'); break; } } diff --git a/tests/MessageTest.php b/tests/MessageTest.php index e7f06cb..eddf11d 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -30,13 +30,18 @@ public function testThrowsExceptionWhenNoRecepientWasAdded() $this->fixture->jsonSerialize(); } - public function testThrowsExceptionWhenMultipleTopicsWereGiven() + public function testWorksCorrectlyWithMultipleTopics() { - $this->setExpectedException(\UnexpectedValueException::class); - $this->fixture->addRecipient(new Topic('breaking-news')) - ->addRecipient(new Topic('another topic')); + $body = '{"condition":"\'topic1\' in topics || \'topic2\' in topics","data":{"foo":"bar"},"priority":"high"}'; - $this->fixture->jsonSerialize(); + $this->fixture->addRecipient(new Topic('topic1')) + ->addRecipient(new Topic('topic2')) + ->setData(['foo' => 'bar']); + + $this->assertSame( + $body, + json_encode($this->fixture) + ); } public function testJsonEncodeWorksOnTopicRecipients() @@ -44,13 +49,12 @@ public function testJsonEncodeWorksOnTopicRecipients() $body = '{"to":"\/topics\/breaking-news","priority":"high","notification":{"title":"test","body":"a nice testing notification"}}'; $notification = new Notification('test', 'a nice testing notification'); - $message = new Message(); - $message->setNotification($notification); + $this->fixture->setNotification($notification); - $message->addRecipient(new Topic('breaking-news')); + $this->fixture->addRecipient(new Topic('breaking-news')); $this->assertSame( $body, - json_encode($message) + json_encode($this->fixture) ); } @@ -59,13 +63,12 @@ public function testJsonEncodeWorksOnDeviceRecipients() $body = '{"to":"deviceId","priority":"high","notification":{"title":"test","body":"a nice testing notification"}}'; $notification = new Notification('test', 'a nice testing notification'); - $message = new Message(); - $message->setNotification($notification); + $this->fixture->setNotification($notification); - $message->addRecipient(new Device('deviceId')); + $this->fixture->addRecipient(new Device('deviceId')); $this->assertSame( $body, - json_encode($message) + json_encode($this->fixture) ); } @@ -73,15 +76,14 @@ public function testJsonEncodeCorrectlyHandlesCollapseKeyAndData() { $body = '{"to":"\/topics\/testing","collapse_key":"collapseMe","data":{"foo":"bar"},"priority":"normal"}'; - $message = new Message(); - $message->setData(['foo' => 'bar']) + $this->fixture->setData(['foo' => 'bar']) ->setCollapseKey('collapseMe') ->setPriority(Message::PRIORITY_NORMAL); - $message->addRecipient(new Topic('testing')); + $this->fixture->addRecipient(new Topic('testing')); $this->assertSame( - $body, - json_encode($message) - ); + $body, + json_encode($this->fixture) + ); } }