Skip to content

feat(fcm): Support apns.live_activity_token field in FCM ApnsConfig #2891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions etc/firebase-admin.messaging.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface AndroidNotification {

// @public
export interface ApnsConfig {
live_activity_token?: string;
fcmOptions?: ApnsFcmOptions;
headers?: {
[key: string]: string;
Expand Down
4 changes: 4 additions & 0 deletions src/messaging/messaging-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,10 @@ export interface WebpushNotification {
* Apple documentation} for various headers and payload fields supported by APNs.
*/
export interface ApnsConfig {
/**
* APN `live_activity_push_to_start_token` or `live_activity_push_token` to start or update live activities.
*/
live_activity_token?: string;
/**
* A collection of APNs headers. Header values must be strings.
*/
Expand Down
17 changes: 17 additions & 0 deletions src/messaging/messaging-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,28 @@ function validateApnsConfig(config: ApnsConfig | undefined): void {
throw new FirebaseMessagingError(
MessagingClientErrorCode.INVALID_PAYLOAD, 'apns must be a non-null object');
}
validateApnsLiveActivityToken(config.live_activity_token);
validateStringMap(config.headers, 'apns.headers');
validateApnsPayload(config.payload);
validateApnsFcmOptions(config.fcmOptions);
}

function validateApnsLiveActivityToken(liveActivityToken: ApnsConfig['live_activity_token']): void {
if (typeof liveActivityToken === 'undefined') {
return;
} else if (!validator.isString(liveActivityToken)) {
throw new FirebaseMessagingError(
MessagingClientErrorCode.INVALID_PAYLOAD,
'apns.live_activity_token must be a string value',
);
} else if (!validator.isNonEmptyString(liveActivityToken)) {
throw new FirebaseMessagingError(
MessagingClientErrorCode.INVALID_PAYLOAD,
'apns.live_activity_token must be a non-empty string',
);
}
}

/**
* Checks if the given ApnsFcmOptions object is valid.
*
Expand Down
15 changes: 15 additions & 0 deletions test/unit/messaging/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,21 @@ describe('Messaging', () => {
});
});

const invalidApnsLiveActivityTokens: any[] = [null, NaN, 0, 1, true, false]

Choose a reason for hiding this comment

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

perhaps we should add a test for valid payload?

invalidApnsLiveActivityTokens.forEach((arg) => {
it(`should throw given invalid apns live activity token: ${JSON.stringify(arg)}`, () => {
expect(() => {
messaging.send({ apns: { live_activity_token: arg }, topic: 'test' });
}).to.throw('apns.live_activity_token must be a string value');
});
})

it('should throw given empty apns live activity token', () => {
expect(() => {
messaging.send({ apns: { live_activity_token: '' }, topic: 'test' });
}).to.throw('apns.live_activity_token must be a non-empty string');
});

const invalidApnsPayloads: any[] = [null, '', 'payload', true, 1.23];
invalidApnsPayloads.forEach((payload) => {
it(`should throw given APNS payload with invalid object: ${JSON.stringify(payload)}`, () => {
Expand Down