Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/ApnAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function adapt(ApnMessage $message, string $token): Notification
{
$alert = null;

if ($message->pushType !== ApnMessagePushType::Background) {
if ($message->pushType !== ApnMessagePushType::Background && $message->pushType !== ApnMessagePushType::LiveActivity) {
$alert = Alert::create();

if ($title = $message->title) {
Expand Down Expand Up @@ -61,6 +61,30 @@ public function adapt(ApnMessage $message, string $token): Notification
$payload->setMutableContent((bool) $mutableContent);
}

if ($contentState = $message->contentState) {
$payload->setContentState($contentState);
}

if ($event = $message->event) {
$payload->setEvent($event);
}

if ($timestamp = $message->timestamp) {
$payload->setTimestamp($timestamp);
}

if ($attributesType = $message->attributesType) {
$payload->setAttributesType($attributesType);
}

if (! empty($message->attributes)) {
$payload->addAttributes($message->attributes);
}

if ($dismissalDate = $message->dismissalDate) {
$payload->setDismissalDate($dismissalDate);
}

if (is_int($badge = $message->badge)) {
$payload->setBadge($badge);
}
Expand Down
100 changes: 100 additions & 0 deletions src/ApnMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,36 @@ class ApnMessage
*/
public ?int $mutableContent = null;

/**
* The content state for live activities.
*/
public ?array $contentState = null;

/**
* The event for live activities.
*/
public ?string $event = null;

/**
* The timestamp for live activities.
*/
public ?int $timestamp = null;

/**
* The attributes type for live activities.
*/
public ?string $attributesType = null;

/**
* The attributes for live activities.
*/
public array $attributes = [];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern with things like this (and perhaps timestamp too) is it it's not clear that these are specific to live activities at all without reading the comment.

Is there instead a possibility that a Live Activity should be represented by a different class than an ApnMessage? Am I right in thinking that when sending a live activity update you aren't sending any of the normal ApnMessage attributes?


/**
* The dismissal date for live activities.
*/
public ?int $dismissalDate = null;

/**
* Custom alert for Edamov/Pushok.
*
Expand Down Expand Up @@ -434,4 +464,74 @@ public function mutableContent(?int $value = 1): self

return $this;
}

/**
* Set content state for live activities.
*/
public function contentState(?array $contentState): self
{
$this->contentState = $contentState;

return $this;
}

/**
* Set event for live activities.
*/
public function event(?string $event): self
{
$this->event = $event;

return $this;
}

/**
* Set timestamp for live activities.
*/
public function timestamp(?int $timestamp): self
{
$this->timestamp = $timestamp;

return $this;
}

/**
* Set attributes type for live activities.
*/
public function attributesType(?string $attributesType): self
{
$this->attributesType = $attributesType;

return $this;
}

/**
* Add an attribute for live activities.
*/
public function attribute(string $key, mixed $value): self
{
$this->attributes[$key] = $value;

return $this;
}

/**
* Set attributes for live activities.
*/
public function setAttributes(array $attributes): self
{
$this->attributes = $attributes;

return $this;
}

/**
* Set dismissal date for live activities.
*/
public function dismissalDate(?int $dismissalDate): self
{
$this->dismissalDate = $dismissalDate;

return $this;
}
}
119 changes: 119 additions & 0 deletions tests/ApnAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,116 @@ public function test_it_does_not_set_mutable_content_by_default()
$this->assertNull($notification->getPayload()->hasMutableContent());
}

public function test_it_adapts_content_state()
{
$contentState = ['status' => 'active', 'count' => 5];
$message = (new ApnMessage)->contentState($contentState);

$notification = $this->adapter->adapt($message, 'token');

$this->assertEquals($contentState, $notification->getPayload()->getContentState());
}

public function test_it_does_not_set_content_state_by_default()
{
$message = (new ApnMessage);

$notification = $this->adapter->adapt($message, 'token');

$this->assertNull($notification->getPayload()->getContentState());
}

public function test_it_adapts_event()
{
$message = (new ApnMessage)->event('update');

$notification = $this->adapter->adapt($message, 'token');

$this->assertEquals('update', $notification->getPayload()->getEvent());
}

public function test_it_does_not_set_event_by_default()
{
$message = (new ApnMessage);

$notification = $this->adapter->adapt($message, 'token');

$this->assertNull($notification->getPayload()->getEvent());
}

public function test_it_adapts_timestamp()
{
$message = (new ApnMessage)->timestamp(1234567890);

$notification = $this->adapter->adapt($message, 'token');

$this->assertEquals(1234567890, $notification->getPayload()->getTimestamp());
}

public function test_it_does_not_set_timestamp_by_default()
{
$message = (new ApnMessage);

$notification = $this->adapter->adapt($message, 'token');

$this->assertNull($notification->getPayload()->getTimestamp());
}

public function test_it_adapts_attributes_type()
{
$message = (new ApnMessage)->attributesType('dateTime');

$notification = $this->adapter->adapt($message, 'token');

$this->assertEquals('dateTime', $notification->getPayload()->getAttributesType());
}

public function test_it_does_not_set_attributes_type_by_default()
{
$message = (new ApnMessage);

$notification = $this->adapter->adapt($message, 'token');

$this->assertNull($notification->getPayload()->getAttributesType());
}

public function test_it_adapts_attributes()
{
$attributes = ['status' => 'active', 'count' => 5];
$message = (new ApnMessage)->setAttributes($attributes);

$notification = $this->adapter->adapt($message, 'token');

$this->assertEquals($attributes, $notification->getPayload()->getAttributes());
}

public function test_it_does_not_set_attributes_by_default()
{
$message = (new ApnMessage);

$notification = $this->adapter->adapt($message, 'token');

$this->assertEquals([], $notification->getPayload()->getAttributes());
}

public function test_it_adapts_dismissal_date()
{
$message = (new ApnMessage)->dismissalDate(1700000000);

$notification = $this->adapter->adapt($message, 'token');

$this->assertEquals(1700000000, $notification->getPayload()->getDismissalDate());
}

public function test_it_does_not_set_dismissal_date_by_default()
{
$message = (new ApnMessage);

$notification = $this->adapter->adapt($message, 'token');

$this->assertNull($notification->getPayload()->getDismissalDate());
}

public function test_it_adapts_badge()
{
$message = new ApnMessage()->badge(1);
Expand Down Expand Up @@ -226,4 +336,13 @@ public function test_it_adapts_background_without_alert(): void

$this->assertNull($notification->getPayload()->getAlert());
}

public function test_it_adapts_live_activity_without_alert(): void
{
$message = (new ApnMessage)->pushType('liveactivity');

$notification = $this->adapter->adapt($message, 'token');

$this->assertNull($notification->getPayload()->getAlert());
}
}
Loading