Skip to content

Commit 4c9dd29

Browse files
authored
Merge pull request #153 from strebl/content-state-support
Add Live Activities Support for iOS Push Notifications
2 parents 30fa05a + bae85ef commit 4c9dd29

File tree

4 files changed

+404
-1
lines changed

4 files changed

+404
-1
lines changed

src/ApnAdapter.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function adapt(ApnMessage $message, string $token): Notification
1515
{
1616
$alert = null;
1717

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

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

64+
if ($contentState = $message->contentState) {
65+
$payload->setContentState($contentState);
66+
}
67+
68+
if ($event = $message->event) {
69+
$payload->setEvent($event);
70+
}
71+
72+
if ($timestamp = $message->timestamp) {
73+
$payload->setTimestamp($timestamp);
74+
}
75+
76+
if ($attributesType = $message->attributesType) {
77+
$payload->setAttributesType($attributesType);
78+
}
79+
80+
if (! empty($message->attributes)) {
81+
$payload->addAttributes($message->attributes);
82+
}
83+
84+
if ($dismissalDate = $message->dismissalDate) {
85+
$payload->setDismissalDate($dismissalDate);
86+
}
87+
6488
if (is_int($badge = $message->badge)) {
6589
$payload->setBadge($badge);
6690
}

src/ApnMessage.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,36 @@ class ApnMessage
124124
*/
125125
public ?int $mutableContent = null;
126126

127+
/**
128+
* The content state for live activities.
129+
*/
130+
public ?array $contentState = null;
131+
132+
/**
133+
* The event for live activities.
134+
*/
135+
public ?string $event = null;
136+
137+
/**
138+
* The timestamp for live activities.
139+
*/
140+
public ?int $timestamp = null;
141+
142+
/**
143+
* The attributes type for live activities.
144+
*/
145+
public ?string $attributesType = null;
146+
147+
/**
148+
* The attributes for live activities.
149+
*/
150+
public array $attributes = [];
151+
152+
/**
153+
* The dismissal date for live activities.
154+
*/
155+
public ?int $dismissalDate = null;
156+
127157
/**
128158
* Custom alert for Edamov/Pushok.
129159
*
@@ -434,4 +464,74 @@ public function mutableContent(?int $value = 1): self
434464

435465
return $this;
436466
}
467+
468+
/**
469+
* Set content state for live activities.
470+
*/
471+
public function contentState(?array $contentState): self
472+
{
473+
$this->contentState = $contentState;
474+
475+
return $this;
476+
}
477+
478+
/**
479+
* Set event for live activities.
480+
*/
481+
public function event(?string $event): self
482+
{
483+
$this->event = $event;
484+
485+
return $this;
486+
}
487+
488+
/**
489+
* Set timestamp for live activities.
490+
*/
491+
public function timestamp(?int $timestamp): self
492+
{
493+
$this->timestamp = $timestamp;
494+
495+
return $this;
496+
}
497+
498+
/**
499+
* Set attributes type for live activities.
500+
*/
501+
public function attributesType(?string $attributesType): self
502+
{
503+
$this->attributesType = $attributesType;
504+
505+
return $this;
506+
}
507+
508+
/**
509+
* Add an attribute for live activities.
510+
*/
511+
public function attribute(string $key, mixed $value): self
512+
{
513+
$this->attributes[$key] = $value;
514+
515+
return $this;
516+
}
517+
518+
/**
519+
* Set attributes for live activities.
520+
*/
521+
public function setAttributes(array $attributes): self
522+
{
523+
$this->attributes = $attributes;
524+
525+
return $this;
526+
}
527+
528+
/**
529+
* Set dismissal date for live activities.
530+
*/
531+
public function dismissalDate(?int $dismissalDate): self
532+
{
533+
$this->dismissalDate = $dismissalDate;
534+
535+
return $this;
536+
}
437537
}

tests/ApnAdapterTest.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,116 @@ public function test_it_does_not_set_mutable_content_by_default()
8888
$this->assertNull($notification->getPayload()->hasMutableContent());
8989
}
9090

91+
public function test_it_adapts_content_state()
92+
{
93+
$contentState = ['status' => 'active', 'count' => 5];
94+
$message = (new ApnMessage)->contentState($contentState);
95+
96+
$notification = $this->adapter->adapt($message, 'token');
97+
98+
$this->assertEquals($contentState, $notification->getPayload()->getContentState());
99+
}
100+
101+
public function test_it_does_not_set_content_state_by_default()
102+
{
103+
$message = (new ApnMessage);
104+
105+
$notification = $this->adapter->adapt($message, 'token');
106+
107+
$this->assertNull($notification->getPayload()->getContentState());
108+
}
109+
110+
public function test_it_adapts_event()
111+
{
112+
$message = (new ApnMessage)->event('update');
113+
114+
$notification = $this->adapter->adapt($message, 'token');
115+
116+
$this->assertEquals('update', $notification->getPayload()->getEvent());
117+
}
118+
119+
public function test_it_does_not_set_event_by_default()
120+
{
121+
$message = (new ApnMessage);
122+
123+
$notification = $this->adapter->adapt($message, 'token');
124+
125+
$this->assertNull($notification->getPayload()->getEvent());
126+
}
127+
128+
public function test_it_adapts_timestamp()
129+
{
130+
$message = (new ApnMessage)->timestamp(1234567890);
131+
132+
$notification = $this->adapter->adapt($message, 'token');
133+
134+
$this->assertEquals(1234567890, $notification->getPayload()->getTimestamp());
135+
}
136+
137+
public function test_it_does_not_set_timestamp_by_default()
138+
{
139+
$message = (new ApnMessage);
140+
141+
$notification = $this->adapter->adapt($message, 'token');
142+
143+
$this->assertNull($notification->getPayload()->getTimestamp());
144+
}
145+
146+
public function test_it_adapts_attributes_type()
147+
{
148+
$message = (new ApnMessage)->attributesType('dateTime');
149+
150+
$notification = $this->adapter->adapt($message, 'token');
151+
152+
$this->assertEquals('dateTime', $notification->getPayload()->getAttributesType());
153+
}
154+
155+
public function test_it_does_not_set_attributes_type_by_default()
156+
{
157+
$message = (new ApnMessage);
158+
159+
$notification = $this->adapter->adapt($message, 'token');
160+
161+
$this->assertNull($notification->getPayload()->getAttributesType());
162+
}
163+
164+
public function test_it_adapts_attributes()
165+
{
166+
$attributes = ['status' => 'active', 'count' => 5];
167+
$message = (new ApnMessage)->setAttributes($attributes);
168+
169+
$notification = $this->adapter->adapt($message, 'token');
170+
171+
$this->assertEquals($attributes, $notification->getPayload()->getAttributes());
172+
}
173+
174+
public function test_it_does_not_set_attributes_by_default()
175+
{
176+
$message = (new ApnMessage);
177+
178+
$notification = $this->adapter->adapt($message, 'token');
179+
180+
$this->assertEquals([], $notification->getPayload()->getAttributes());
181+
}
182+
183+
public function test_it_adapts_dismissal_date()
184+
{
185+
$message = (new ApnMessage)->dismissalDate(1700000000);
186+
187+
$notification = $this->adapter->adapt($message, 'token');
188+
189+
$this->assertEquals(1700000000, $notification->getPayload()->getDismissalDate());
190+
}
191+
192+
public function test_it_does_not_set_dismissal_date_by_default()
193+
{
194+
$message = (new ApnMessage);
195+
196+
$notification = $this->adapter->adapt($message, 'token');
197+
198+
$this->assertNull($notification->getPayload()->getDismissalDate());
199+
}
200+
91201
public function test_it_adapts_badge()
92202
{
93203
$message = new ApnMessage()->badge(1);
@@ -226,4 +336,13 @@ public function test_it_adapts_background_without_alert(): void
226336

227337
$this->assertNull($notification->getPayload()->getAlert());
228338
}
339+
340+
public function test_it_adapts_live_activity_without_alert(): void
341+
{
342+
$message = (new ApnMessage)->pushType('liveactivity');
343+
344+
$notification = $this->adapter->adapt($message, 'token');
345+
346+
$this->assertNull($notification->getPayload()->getAlert());
347+
}
229348
}

0 commit comments

Comments
 (0)