Skip to content

Commit bbb7e6b

Browse files
committed
Enable setting a live activity token in an Apns config
1 parent 13684f2 commit bbb7e6b

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Please read about the future of the Firebase Admin PHP SDK on the
1212
* You can now save on method call by passing a custom Firestore database name to
1313
`Kreait\Firebase\Factory::createFirestore($databaseName)` instead of having to chain
1414
``::withFirestoreDatabase($databaseName)->createFirestore()``
15+
* It is now possible to set [live activity tokens](https://firebase.google.com/docs/cloud-messaging/ios/live-activity)
16+
in Apns configs.
1517

1618
### Deprecated
1719

src/Firebase/Messaging/ApnsConfig.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
* fcm_options?: array{
2222
* analytics_label?: string,
2323
* image?: string
24-
* }
24+
* },
25+
* live_activity_token?: non-empty-string
2526
* }
2627
*/
2728
final class ApnsConfig implements JsonSerializable
@@ -34,17 +35,19 @@ final class ApnsConfig implements JsonSerializable
3435
* @param array<non-empty-string, non-empty-string> $headers
3536
* @param array<non-empty-string, mixed> $payload
3637
* @param array<non-empty-string, string> $fcmOptions
38+
* @param non-empty-string|null $liveActivityToken
3739
*/
3840
private function __construct(
3941
private readonly array $headers,
4042
private readonly array $payload,
4143
private readonly array $fcmOptions,
44+
private readonly ?string $liveActivityToken,
4245
) {
4346
}
4447

4548
public static function new(): self
4649
{
47-
return new self([], [], []);
50+
return new self([], [], [], null);
4851
}
4952

5053
/**
@@ -55,8 +58,9 @@ public static function fromArray(array $data): self
5558
$headers = $data['headers'] ?? [];
5659
$payload = $data['payload'] ?? [];
5760
$fcmOptions = $data['fcm_options'] ?? [];
61+
$liveActivityToken = $data['live_activity_token'] ?? null;
5862

59-
return new self($headers, $payload, $fcmOptions);
63+
return new self($headers, $payload, $fcmOptions, $liveActivityToken);
6064
}
6165

6266
/**
@@ -72,6 +76,7 @@ public function withHeader(string $name, string $value): self
7276
headers: $headers,
7377
payload: $this->payload,
7478
fcmOptions: $this->fcmOptions,
79+
liveActivityToken: $this->liveActivityToken,
7580
);
7681
}
7782

@@ -93,6 +98,7 @@ public function withApsField(string $key, mixed $value): self
9398
headers: $this->headers,
9499
payload: $payload,
95100
fcmOptions: $this->fcmOptions,
101+
liveActivityToken: $this->liveActivityToken,
96102
);
97103
}
98104

@@ -112,6 +118,7 @@ public function withDataField(string $name, mixed $value): self
112118
headers: $this->headers,
113119
payload: $payload,
114120
fcmOptions: $this->fcmOptions,
121+
liveActivityToken: $this->liveActivityToken,
115122
);
116123
}
117124

@@ -155,6 +162,20 @@ public function withPriority(string $priority): self
155162
return $this->withHeader('apns-priority', $priority);
156163
}
157164

165+
/**
166+
* @see https://firebase.google.com/docs/cloud-messaging/ios/live-activity
167+
* @param non-empty-string $liveActivityToken
168+
*/
169+
public function withLiveActivityToken(string $liveActivityToken): self
170+
{
171+
return new self(
172+
headers: $this->headers,
173+
payload: $this->payload,
174+
fcmOptions: $this->fcmOptions,
175+
liveActivityToken: $liveActivityToken,
176+
);
177+
}
178+
158179
/**
159180
* A subtitle of the notification, supported by iOS 9+, silently ignored for others.
160181
*/
@@ -194,6 +215,7 @@ public function toArray(): array
194215
'headers' => array_filter($this->headers, $filter),
195216
'payload' => array_filter($this->payload, $filter),
196217
'fcm_options' => array_filter($this->fcmOptions, $filter),
218+
'live_activity_token' => $this->liveActivityToken,
197219
], $filter);
198220
}
199221

tests/Unit/Messaging/ApnsConfigTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ public function itCanHaveAPowerConservingPriority(): void
8989
$this->assertSame('5', $payload['headers']['apns-priority']);
9090
}
9191

92+
#[Test]
93+
public function itCanBeGivenALiveActivityToken(): void
94+
{
95+
$config = ApnsConfig::fromArray(['live_activity_token' => 'token']);
96+
97+
$payload = Json::decode(Json::encode($config), true);
98+
99+
$this->assertArrayHasKey('live_activity_token', $payload);
100+
$this->assertSame('token', $payload['live_activity_token']);
101+
}
102+
92103
#[Test]
93104
public function itHasASubtitle(): void
94105
{
@@ -123,6 +134,7 @@ public static function validDataProvider(): Iterator
123134
'sound' => 'default',
124135
],
125136
],
137+
'live_activity_token' => 'token',
126138
]];
127139
}
128140
}

0 commit comments

Comments
 (0)