Skip to content

Commit 49876eb

Browse files
authored
Merge pull request #18 from IIISpikerIII/master
Add multiple send topics with &&
2 parents 8979794 + 3c41f80 commit 49876eb

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
vendor
22
.project
33
.buildpath
4-
.settings
4+
.settings

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ php:
77
- nightly
88

99
before_script:
10-
composer install
10+
composer up
1111

1212
script:
1313
- mkdir -p build/logs

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ $client->injectHttpClient(new \GuzzleHttp\Client());
7575

7676
$message = new Message();
7777
$message->addRecipient(new Topic('your-topic'));
78+
//select devices where has 'your-topic1' && 'your-topic2' topics
79+
$message->addRecipient(new Topic(['your-topic1', 'your-topic2']));
7880
$message->setNotification(new Notification('test title', 'testing body'))
7981
->setData(array('someId' => 111));
8082

src/Message.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,25 @@ private function createTo(array &$jsonData)
177177
{
178178
switch ($this->recipientType) {
179179
case Topic::class:
180-
if (count($this->recipients) > 1) {
180+
if (count($this->recipients) > 1 || is_array(current($this->recipients)->getIdentifier())) {
181181
$topics = array_map(
182-
function (Topic $topic) { return sprintf("'%s' in topics", $topic->getIdentifier()); },
182+
function (Topic $topic) {
183+
$identity = $topic->getIdentifier();
184+
185+
if (is_array($identity)) {
186+
$conditions = array_map(function ($condition) {
187+
return sprintf("'%s' in topics", $condition);
188+
}, $identity);
189+
190+
return '(' . implode(' && ', $conditions) . ')';
191+
} else {
192+
return sprintf("'%s' in topics", $topic->getIdentifier());
193+
}
194+
},
183195
$this->recipients
184196
);
185197
$jsonData['condition'] = implode(' || ', $topics);
186-
break;
198+
return;
187199
}
188200
$jsonData['to'] = sprintf('/topics/%s', current($this->recipients)->getIdentifier());
189201
break;

tests/MessageTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ public function testWorksCorrectlyWithMultipleTopics()
4444
);
4545
}
4646

47+
public function testWorksCorrectlyWithMultipleTopicsAnd()
48+
{
49+
$body = '{"condition":"(\'topic1\' in topics && \'topic2\' in topics)","data":{"foo":"bar"},"priority":"high"}';
50+
51+
$this->fixture->addRecipient(new Topic(['topic1','topic2']))
52+
->setData(['foo' => 'bar']);
53+
54+
$this->assertSame(
55+
$body,
56+
json_encode($this->fixture)
57+
);
58+
}
59+
4760
public function testJsonEncodeWorksOnTopicRecipients()
4861
{
4962
$body = '{"to":"\/topics\/breaking-news","priority":"high","notification":{"title":"test","body":"a nice testing notification"}}';

0 commit comments

Comments
 (0)