Skip to content

Commit

Permalink
Merge pull request #182 from danielsimkus/add-attribute-support-for-l…
Browse files Browse the repository at this point in the history
…ive-activity-widgets

Add attribute support for live activity widgets
  • Loading branch information
edamov authored Feb 3, 2025
2 parents bb2a0ad + f907bf1 commit 52dae5b
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/Payload.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class Payload implements \JsonSerializable
const PAYLOAD_RELEVANCE_SCORE_KEY = 'relevance-score';
const PAYLOAD_STALE_DATE_KEY = 'stale-date';
const PAYLOAD_CONTENT_STATE_KEY = 'content-state';
const PAYLOAD_DISMISSAL_DATE_KEY = 'dismissal-date';
const PAYLOAD_ATTRIBUTES_TYPE_KEY = 'attributes-type';
const PAYLOAD_ATTRIBUTES_KEY = 'attributes';

const PAYLOAD_HTTP2_REGULAR_NOTIFICATION_MAXIMUM_SIZE = 4096;
const PAYLOAD_HTTP2_VOIP_NOTIFICATION_MAXIMUM_SIZE = 5120;
Expand Down Expand Up @@ -169,6 +172,27 @@ class Payload implements \JsonSerializable
*/
private $contentState;

/**
* Attributes type
*
* @var string|null
*/
private $attributesType;

/**
* Attributes
*
* @var array
*/
private $attributes = [];

/**
* Dismissal date
*
* @var int|null
*/
private $dismissalDate;

protected function __construct()
{
}
Expand Down Expand Up @@ -595,6 +619,79 @@ public function getEvent()
return $this->event;
}

/**
* Set attributes type for Payload.
* This is used to specify the interpreter of the attributes on the apple side.
*
* @param string $attributesType
* @return Payload
*/
public function setAttributesType(string $attributesType): self
{
$this->attributesType = $attributesType;

return $this;
}

/**
* Get attributes type for Payload.
*
* @return string|null
*/
public function getAttributesType(): string|null
{
return $this->attributesType;
}

/**
* Add an attribute to the payload.
*
* @param string $key
* @param mixed $value
* @return Payload
*/
public function addAttribute(string $key, mixed $value): Payload
{
$this->attributes[$key] = $value;

return $this;
}

/**
* Add an array of attributes to the payload.
*
* @param array $attributes
* @return Payload
*/
public function addAttributes(array $attributes): Payload
{
$this->attributes = array_merge($this->attributes, $attributes);

return $this;
}

/**
* Get Attributes
*
* @return array
*/
public function getAttributes(): array
{
return $this->attributes;
}

public function setDismissalDate(int $value): Payload
{
$this->dismissalDate = $value;

return $this;
}

public function getDismissalDate(): int|null
{
return $this->dismissalDate;
}

/**
* Convert Payload to JSON.
*
Expand Down Expand Up @@ -688,6 +785,15 @@ public function jsonSerialize(): array
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_RELEVANCE_SCORE_KEY} = $this->relevanceScore;
}

if ($this->dismissalDate) {
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_DISMISSAL_DATE_KEY} = (int) $this->getDismissalDate();
}

if ($this->attributesType) {
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ATTRIBUTES_TYPE_KEY} = $this->attributesType;
$payload[self::PAYLOAD_ROOT_KEY]->{self::PAYLOAD_ATTRIBUTES_KEY} = $this->attributes;
}

return $payload;
}

Expand Down
63 changes: 63 additions & 0 deletions tests/PayloadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,69 @@ public function testGetCustomValueOfNotExistingKey()
->getCustomValue('notExistingKey', 'value');
}

public function testSetDismissalDate()
{
$payload = Payload::create()->setDismissalDate(123456789);

$this->assertEquals(123456789, $payload->getDismissalDate());
}

public function testDismissalDateJson()
{
$payload = Payload::create()
->setDismissalDate(123456789);
$this->assertJsonStringEqualsJsonString(
'{"aps":{"dismissal-date":123456789}}',
$payload->toJson()
);
}

public function testSetAttributesType()
{
$payload = Payload::create()->setAttributesType('attributesType');

$this->assertEquals('attributesType', $payload->getAttributesType());
}

public function testAddAttribute()
{
$payload = Payload::create()
->addAttribute('key', 'value')
->addAttribute('key2', 'value2');

$this->assertEquals(['key' => 'value', 'key2' => 'value2'], $payload->getAttributes());
}

public function testAddAttributesMergesWithExisting()
{
$payload = Payload::create()
->addAttributes(['key3' => 'value3', 'key' => 'replaced'])
->addAttributes(['key' => 'value', 'key2' => 'value2']);

$this->assertEquals(['key' => 'value', 'key2' => 'value2', 'key3' => 'value3'], $payload->getAttributes());
}

public function testAttributesJsonSerializeCorrectly()
{
$payload = Payload::create()
->setAttributesType('attributesType')
->addAttributes(['key' => 'value', 'key2' => 'value2']);
$this->assertJsonStringEqualsJsonString(
'{"aps":{"attributes-type":"attributesType","attributes":{"key":"value","key2":"value2"}}}',
$payload->toJson()
);
}

public function testAttributesIgnoredIfNoAttributesType()
{
$payload = Payload::create()
->addAttributes(['key' => 'value', 'key2' => 'value2']);
$this->assertJsonStringEqualsJsonString(
'{"aps":{}}',
$payload->toJson()
);
}

public function testSetPushType()
{
$payload = Payload::create()->setPushType('pushType');
Expand Down

0 comments on commit 52dae5b

Please sign in to comment.