Skip to content

Commit 08ce6f1

Browse files
redjanymredjanym
authored andcommitted
Add Multiple Devices support
1 parent 1e80912 commit 08ce6f1

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

src/Client.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,8 @@ protected function processTopicSubscription($topic_id, $recipients_tokens, $url)
123123
);
124124
}
125125

126-
127126
private function getApiUrl()
128127
{
129128
return isset($this->proxyApiUrl) ? $this->proxyApiUrl : self::DEFAULT_API_URL;
130129
}
131-
}
130+
}

src/ClientInterface.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,3 @@ function setProxyApiUrl($url);
4343
function send(Message $message);
4444

4545
}
46-

src/Message.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function addRecipient(Recipient $recipient)
3838
$this->recipientType = get_class($recipient);
3939
}
4040
if ($this->recipientType !== get_class($recipient)) {
41-
throw new \InvalidArgumentException('mixed recepient types are not supported by FCM');
41+
throw new \InvalidArgumentException('Mixed recipient types are not supported by FCM');
4242
}
4343

4444
return $this;
@@ -142,7 +142,14 @@ public function jsonSerialize()
142142
throw new \UnexpectedValueException('Message must have at least one recipient');
143143
}
144144

145-
$jsonData['to'] = $this->createTo();
145+
$target = $this->createTo();
146+
147+
if(is_array($target)){
148+
$jsonData['registration_ids'] = $target;
149+
} else {
150+
$jsonData['to'] = $target;
151+
}
152+
146153
if ($this->collapseKey) {
147154
$jsonData['collapse_key'] = $this->collapseKey;
148155
}
@@ -174,13 +181,16 @@ private function createTo()
174181
case Device::class:
175182
if (count($this->recipients) == 1) {
176183
return current($this->recipients)->getToken();
184+
} else {
185+
return array_map(function($device){
186+
return $device->getToken();
187+
}, $this->recipients);
177188
}
178189

179190
break;
180191
default:
181192
throw new \UnexpectedValueException('PhpFirebaseCloudMessaging only supports single topic and single device messages yet');
182193
break;
183194
}
184-
return null;
185195
}
186-
}
196+
}

tests/ClientTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function testSendConstruesValidJsonForNotificationWithTopic()
3232
->with(Client::DEFAULT_API_URL, array('headers' => $headers, 'body' => '{"to":"\\/topics\\/test"}'))
3333
->andReturn(\Mockery::mock(Response::class));
3434

35-
$this->fixture->injectGuzzleHttpClient($guzzle);
3635
$this->fixture->setApiKey($apiKey);
3736

3837
$message = new Message();

0 commit comments

Comments
 (0)