Skip to content
27 changes: 25 additions & 2 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +29,7 @@ class Message implements \JsonSerializable
private $recipientType;
private $timeToLive;
private $delayWhileIdle;
private $mutableContent;

/**
* Represents the app's "Send-to-Sync" message.
Expand All @@ -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');
}

Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
Expand All @@ -199,4 +222,4 @@ function (Topic $topic) { return sprintf("'%s' in topics", $topic->getIdentifier
}
}
}
}
}
17 changes: 17 additions & 0 deletions src/Recipient/GroupTopic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace paragraph1\phpFCM\Recipient;

class GroupTopic implements Recipient
{
private $name;

public function __construct($name)
{
$this->name = $name;
}

public function getIdentifier()
{
return $this->name;
}
}
12 changes: 12 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down