diff --git a/src/ApnAdapter.php b/src/ApnAdapter.php index 180f68e..41a7996 100644 --- a/src/ApnAdapter.php +++ b/src/ApnAdapter.php @@ -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) { @@ -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); } diff --git a/src/ApnMessage.php b/src/ApnMessage.php index 8daf209..36ba418 100644 --- a/src/ApnMessage.php +++ b/src/ApnMessage.php @@ -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 = []; + + /** + * The dismissal date for live activities. + */ + public ?int $dismissalDate = null; + /** * Custom alert for Edamov/Pushok. * @@ -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; + } } diff --git a/tests/ApnAdapterTest.php b/tests/ApnAdapterTest.php index a6a745f..e8af2dc 100644 --- a/tests/ApnAdapterTest.php +++ b/tests/ApnAdapterTest.php @@ -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); @@ -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()); + } } diff --git a/tests/ApnMessageTest.php b/tests/ApnMessageTest.php index 954b48f..184c08e 100644 --- a/tests/ApnMessageTest.php +++ b/tests/ApnMessageTest.php @@ -145,6 +145,21 @@ public function test_it_can_set_push_type() $this->assertEquals($message, $result); } + public function test_it_has_live_activity_push_type_constant() + { + $this->assertEquals('liveactivity', ApnMessage::PUSH_TYPE_LIVE_ACTIVITY); + } + + public function test_it_can_set_push_type_to_live_activity() + { + $message = new ApnMessage; + + $result = $message->pushType(ApnMessage::PUSH_TYPE_LIVE_ACTIVITY); + + $this->assertEquals('liveactivity', $message->pushType); + $this->assertEquals($message, $result); + } + public function test_it_can_set_expires_at() { $message = new ApnMessage; @@ -315,4 +330,149 @@ public function test_it_can_set_loc_args() $this->assertEquals(['hello', 'world'], $message->locArgs); $this->assertEquals($message, $result); } + + public function test_it_can_set_content_state() + { + $message = new ApnMessage; + + $contentState = ['status' => 'active', 'count' => 5]; + + $result = $message->contentState($contentState); + + $this->assertEquals($contentState, $message->contentState); + $this->assertEquals($message, $result); + } + + public function test_it_can_set_content_state_to_null() + { + $message = new ApnMessage; + + $contentState = ['status' => 'active', 'count' => 5]; + $message->contentState($contentState); + + $result = $message->contentState(null); + + $this->assertEquals(null, $message->contentState); + $this->assertEquals($message, $result); + } + + public function test_it_can_set_event() + { + $message = new ApnMessage; + + $result = $message->event('update'); + + $this->assertEquals('update', $message->event); + $this->assertEquals($message, $result); + } + + public function test_it_can_set_event_to_null() + { + $message = new ApnMessage; + + $message->event('update'); + + $result = $message->event(null); + + $this->assertEquals(null, $message->event); + $this->assertEquals($message, $result); + } + + public function test_it_can_set_timestamp() + { + $message = new ApnMessage; + + $result = $message->timestamp(1234567890); + + $this->assertEquals(1234567890, $message->timestamp); + $this->assertEquals($message, $result); + } + + public function test_it_can_set_timestamp_to_null() + { + $message = new ApnMessage; + + $message->timestamp(1234567890); + + $result = $message->timestamp(null); + + $this->assertEquals(null, $message->timestamp); + $this->assertEquals($message, $result); + } + + public function test_it_can_set_attributes_type() + { + $message = new ApnMessage; + + $result = $message->attributesType('dateTime'); + + $this->assertEquals('dateTime', $message->attributesType); + $this->assertEquals($message, $result); + } + + public function test_it_can_set_attributes_type_to_null() + { + $message = new ApnMessage; + + $message->attributesType('dateTime'); + + $result = $message->attributesType(null); + + $this->assertEquals(null, $message->attributesType); + $this->assertEquals($message, $result); + } + + public function test_it_can_add_attribute() + { + $message = new ApnMessage; + + $result = $message->attribute('key', 'value'); + + $this->assertEquals(['key' => 'value'], $message->attributes); + $this->assertEquals($message, $result); + } + + public function test_it_can_add_multiple_attributes() + { + $message = new ApnMessage; + + $message->attribute('key1', 'value1') + ->attribute('key2', 'value2'); + + $this->assertEquals(['key1' => 'value1', 'key2' => 'value2'], $message->attributes); + } + + public function test_it_can_set_attributes() + { + $message = new ApnMessage; + + $attributes = ['status' => 'active', 'count' => 5]; + + $result = $message->setAttributes($attributes); + + $this->assertEquals($attributes, $message->attributes); + $this->assertEquals($message, $result); + } + + public function test_it_can_set_dismissal_date() + { + $message = new ApnMessage; + + $result = $message->dismissalDate(1700000000); + + $this->assertEquals(1700000000, $message->dismissalDate); + $this->assertEquals($message, $result); + } + + public function test_it_can_set_dismissal_date_to_null() + { + $message = new ApnMessage; + + $message->dismissalDate(1700000000); + + $result = $message->dismissalDate(null); + + $this->assertEquals(null, $message->dismissalDate); + $this->assertEquals($message, $result); + } }