Skip to content

Commit 95a8dec

Browse files
authored
Merge pull request #3 from lutskanu/master
Added time_to_live and delay_while_idle message parameters
2 parents a837afe + 1e890ce commit 95a8dec

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/Message.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Message implements \JsonSerializable
2525
private $data;
2626
private $recipients = array();
2727
private $recipientType;
28+
private $timeToLive;
29+
private $delayWhileIdle;
2830

2931
/**
3032
* where should the message go
@@ -89,6 +91,33 @@ public function setPriority($priority)
8991
return $this;
9092
}
9193

94+
/**
95+
* @see https://firebase.google.com/docs/cloud-messaging/concept-options#ttl
96+
*
97+
* @param integer $ttl
98+
*
99+
* @return \paragraph1\phpFCM\Message
100+
*/
101+
public function setTimeToLive($ttl)
102+
{
103+
$this->timeToLive = $ttl;
104+
105+
return $this;
106+
}
107+
108+
/**
109+
* @see https://firebase.google.com/docs/cloud-messaging/concept-options#lifetime
110+
*
111+
* @param bool $delayWhileIdle
112+
*
113+
* @return \paragraph1\phpFCM\Message
114+
*/
115+
public function setDelayWhileIdle($delayWhileIdle)
116+
{
117+
$this->delayWhileIdle = $delayWhileIdle;
118+
return $this;
119+
}
120+
92121
public function setData(array $data)
93122
{
94123
$this->data = $data;
@@ -116,6 +145,12 @@ public function jsonSerialize()
116145
if ($this->notification) {
117146
$jsonData['notification'] = $this->notification;
118147
}
148+
if ($this->timeToLive) {
149+
$jsonData['time_to_live'] = (int)$this->timeToLive;
150+
}
151+
if ($this->delayWhileIdle) {
152+
$jsonData['delay_while_idle'] = (bool)$this->delayWhileIdle;
153+
}
119154

120155
return $jsonData;
121156
}

tests/MessageTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,36 @@ public function testJsonEncodeCorrectlyHandlesCollapseKeyAndData()
9999
);
100100
}
101101

102+
public function testJsonEncodeHandlesTTL()
103+
{
104+
$body = '{"to":"\/topics\/testing","data":{"foo":"bar"},"priority":"high","time_to_live":3}';
105+
106+
$this->fixture->setData(['foo' => 'bar'])
107+
->setTimeToLive(3);
108+
109+
$this->fixture->addRecipient(new Topic('testing'));
110+
111+
$this->assertSame(
112+
$body,
113+
json_encode($this->fixture)
114+
);
115+
}
116+
117+
public function testJsonEncodeHandlesDelayIdle()
118+
{
119+
$body = '{"to":"\/topics\/testing","data":{"foo":"bar"},"priority":"high","delay_while_idle":true}';
120+
121+
$this->fixture->setData(['foo' => 'bar'])
122+
->setDelayWhileIdle(true);
123+
124+
$this->fixture->addRecipient(new Topic('testing'));
125+
126+
$this->assertSame(
127+
$body,
128+
json_encode($this->fixture)
129+
);
130+
}
131+
102132
public function testAddingNewAndUnknownRecipientTypesYieldsException()
103133
{
104134
$this->setExpectedException(\UnexpectedValueException::class);

0 commit comments

Comments
 (0)