Skip to content

Commit

Permalink
enabled messaging to multiple topics via conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
palbertini committed Jun 2, 2016
1 parent d878e8d commit f6a636b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 11 additions & 9 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
}
Expand Down
40 changes: 21 additions & 19 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,31 @@ 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()
{
$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)
);
}

Expand All @@ -59,29 +63,27 @@ 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)
);
}

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)
);
}
}

0 comments on commit f6a636b

Please sign in to comment.