Skip to content

Commit a78065a

Browse files
committed
feat(fcm): Support apns.live_activity_token field in FCM ApnsConfig
* feat(fcm): Support `live_activity_token` field on `ApnsConfig` * added validation * update documentation
1 parent a46086b commit a78065a

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

etc/firebase-admin.messaging.api.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface AndroidNotification {
5757

5858
// @public
5959
export interface ApnsConfig {
60+
live_activity_token?: string;
6061
fcmOptions?: ApnsFcmOptions;
6162
headers?: {
6263
[key: string]: string;

src/messaging/messaging-api.ts

+4
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ export interface WebpushNotification {
241241
* Apple documentation} for various headers and payload fields supported by APNs.
242242
*/
243243
export interface ApnsConfig {
244+
/**
245+
* APN `live_activity_push_to_start_token` or `live_activity_push_token` to start or update live activities.
246+
*/
247+
live_activity_token?: string;
244248
/**
245249
* A collection of APNs headers. Header values must be strings.
246250
*/

src/messaging/messaging-internal.ts

+17
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,28 @@ function validateApnsConfig(config: ApnsConfig | undefined): void {
123123
throw new FirebaseMessagingError(
124124
MessagingClientErrorCode.INVALID_PAYLOAD, 'apns must be a non-null object');
125125
}
126+
validateApnsLiveActivityToken(config.live_activity_token);
126127
validateStringMap(config.headers, 'apns.headers');
127128
validateApnsPayload(config.payload);
128129
validateApnsFcmOptions(config.fcmOptions);
129130
}
130131

132+
function validateApnsLiveActivityToken(liveActivityToken: ApnsConfig['live_activity_token']): void {
133+
if (typeof liveActivityToken === 'undefined') {
134+
return;
135+
} else if (!validator.isString(liveActivityToken)) {
136+
throw new FirebaseMessagingError(
137+
MessagingClientErrorCode.INVALID_PAYLOAD,
138+
'apns.live_activity_token must be a string value',
139+
);
140+
} else if (!validator.isNonEmptyString(liveActivityToken)) {
141+
throw new FirebaseMessagingError(
142+
MessagingClientErrorCode.INVALID_PAYLOAD,
143+
'apns.live_activity_token must be a non-empty string',
144+
);
145+
}
146+
}
147+
131148
/**
132149
* Checks if the given ApnsFcmOptions object is valid.
133150
*

test/unit/messaging/messaging.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -1725,6 +1725,21 @@ describe('Messaging', () => {
17251725
});
17261726
});
17271727

1728+
const invalidApnsLiveActivityTokens: any[] = [null, NaN, 0, 1, true, false]
1729+
invalidApnsLiveActivityTokens.forEach((arg) => {
1730+
it(`should throw given invalid apns live activity token: ${JSON.stringify(arg)}`, () => {
1731+
expect(() => {
1732+
messaging.send({ apns: { live_activity_token: arg }, topic: 'test' });
1733+
}).to.throw('apns.live_activity_token must be a string value');
1734+
});
1735+
})
1736+
1737+
it('should throw given empty apns live activity token', () => {
1738+
expect(() => {
1739+
messaging.send({ apns: { live_activity_token: '' }, topic: 'test' });
1740+
}).to.throw('apns.live_activity_token must be a non-empty string');
1741+
});
1742+
17281743
const invalidApnsPayloads: any[] = [null, '', 'payload', true, 1.23];
17291744
invalidApnsPayloads.forEach((payload) => {
17301745
it(`should throw given APNS payload with invalid object: ${JSON.stringify(payload)}`, () => {

0 commit comments

Comments
 (0)