Skip to content

Commit 52dae5b

Browse files
authored
Merge pull request #182 from danielsimkus/add-attribute-support-for-live-activity-widgets
Add attribute support for live activity widgets
2 parents bb2a0ad + f907bf1 commit 52dae5b

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed

src/Payload.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ class Payload implements \JsonSerializable
4747
const PAYLOAD_RELEVANCE_SCORE_KEY = 'relevance-score';
4848
const PAYLOAD_STALE_DATE_KEY = 'stale-date';
4949
const PAYLOAD_CONTENT_STATE_KEY = 'content-state';
50+
const PAYLOAD_DISMISSAL_DATE_KEY = 'dismissal-date';
51+
const PAYLOAD_ATTRIBUTES_TYPE_KEY = 'attributes-type';
52+
const PAYLOAD_ATTRIBUTES_KEY = 'attributes';
5053

5154
const PAYLOAD_HTTP2_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 4096;
5255
const PAYLOAD_HTTP2_VOIP_NOTIFICATION_MAXIMUM_SIZE = 5120;
@@ -169,6 +172,27 @@ class Payload implements \JsonSerializable
169172
*/
170173
private $contentState;
171174

175+
/**
176+
* Attributes type
177+
*
178+
* @var string|null
179+
*/
180+
private $attributesType;
181+
182+
/**
183+
* Attributes
184+
*
185+
* @var array
186+
*/
187+
private $attributes = [];
188+
189+
/**
190+
* Dismissal date
191+
*
192+
* @var int|null
193+
*/
194+
private $dismissalDate;
195+
172196
protected function __construct()
173197
{
174198
}
@@ -595,6 +619,79 @@ public function getEvent()
595619
return $this->event;
596620
}
597621

622+
/**
623+
* Set attributes type for Payload.
624+
* This is used to specify the interpreter of the attributes on the apple side.
625+
*
626+
* @param string $attributesType
627+
* @return Payload
628+
*/
629+
public function setAttributesType(string $attributesType): self
630+
{
631+
$this->attributesType = $attributesType;
632+
633+
return $this;
634+
}
635+
636+
/**
637+
* Get attributes type for Payload.
638+
*
639+
* @return string|null
640+
*/
641+
public function getAttributesType(): string|null
642+
{
643+
return $this->attributesType;
644+
}
645+
646+
/**
647+
* Add an attribute to the payload.
648+
*
649+
* @param string $key
650+
* @param mixed $value
651+
* @return Payload
652+
*/
653+
public function addAttribute(string $key, mixed $value): Payload
654+
{
655+
$this->attributes[$key] = $value;
656+
657+
return $this;
658+
}
659+
660+
/**
661+
* Add an array of attributes to the payload.
662+
*
663+
* @param array $attributes
664+
* @return Payload
665+
*/
666+
public function addAttributes(array $attributes): Payload
667+
{
668+
$this->attributes = array_merge($this->attributes, $attributes);
669+
670+
return $this;
671+
}
672+
673+
/**
674+
* Get Attributes
675+
*
676+
* @return array
677+
*/
678+
public function getAttributes(): array
679+
{
680+
return $this->attributes;
681+
}
682+
683+
public function setDismissalDate(int $value): Payload
684+
{
685+
$this->dismissalDate = $value;
686+
687+
return $this;
688+
}
689+
690+
public function getDismissalDate(): int|null
691+
{
692+
return $this->dismissalDate;
693+
}
694+
598695
/**
599696
* Convert Payload to JSON.
600697
*
@@ -688,6 +785,15 @@ public function jsonSerialize(): array
688785
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_RELEVANCE_SCORE_KEY} = $this->relevanceScore;
689786
}
690787

788+
if ($this->dismissalDate) {
789+
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_DISMISSAL_DATE_KEY} = (int) $this->getDismissalDate();
790+
}
791+
792+
if ($this->attributesType) {
793+
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ATTRIBUTES_TYPE_KEY} = $this->attributesType;
794+
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ATTRIBUTES_KEY} = $this->attributes;
795+
}
796+
691797
return $payload;
692798
}
693799

tests/PayloadTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,69 @@ public function testGetCustomValueOfNotExistingKey()
103103
->getCustomValue('notExistingKey', 'value');
104104
}
105105

106+
public function testSetDismissalDate()
107+
{
108+
$payload = Payload::create()->setDismissalDate(123456789);
109+
110+
$this->assertEquals(123456789, $payload->getDismissalDate());
111+
}
112+
113+
public function testDismissalDateJson()
114+
{
115+
$payload = Payload::create()
116+
->setDismissalDate(123456789);
117+
$this->assertJsonStringEqualsJsonString(
118+
'{"aps":{"dismissal-date":123456789}}',
119+
$payload->toJson()
120+
);
121+
}
122+
123+
public function testSetAttributesType()
124+
{
125+
$payload = Payload::create()->setAttributesType('attributesType');
126+
127+
$this->assertEquals('attributesType', $payload->getAttributesType());
128+
}
129+
130+
public function testAddAttribute()
131+
{
132+
$payload = Payload::create()
133+
->addAttribute('key', 'value')
134+
->addAttribute('key2', 'value2');
135+
136+
$this->assertEquals(['key' => 'value', 'key2' => 'value2'], $payload->getAttributes());
137+
}
138+
139+
public function testAddAttributesMergesWithExisting()
140+
{
141+
$payload = Payload::create()
142+
->addAttributes(['key3' => 'value3', 'key' => 'replaced'])
143+
->addAttributes(['key' => 'value', 'key2' => 'value2']);
144+
145+
$this->assertEquals(['key' => 'value', 'key2' => 'value2', 'key3' => 'value3'], $payload->getAttributes());
146+
}
147+
148+
public function testAttributesJsonSerializeCorrectly()
149+
{
150+
$payload = Payload::create()
151+
->setAttributesType('attributesType')
152+
->addAttributes(['key' => 'value', 'key2' => 'value2']);
153+
$this->assertJsonStringEqualsJsonString(
154+
'{"aps":{"attributes-type":"attributesType","attributes":{"key":"value","key2":"value2"}}}',
155+
$payload->toJson()
156+
);
157+
}
158+
159+
public function testAttributesIgnoredIfNoAttributesType()
160+
{
161+
$payload = Payload::create()
162+
->addAttributes(['key' => 'value', 'key2' => 'value2']);
163+
$this->assertJsonStringEqualsJsonString(
164+
'{"aps":{}}',
165+
$payload->toJson()
166+
);
167+
}
168+
106169
public function testSetPushType()
107170
{
108171
$payload = Payload::create()->setPushType('pushType');

0 commit comments

Comments
 (0)