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 3 commits into
base: master
Choose a base branch
from
Open
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
@@ -61,6 +61,7 @@ export interface ApnsConfig {
headers?: {
[key: string]: string;
};
liveActivityToken?: string;
payload?: ApnsPayload;
}

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

const propertyMappings = {
liveActivityToken: 'live_activity_token'
}

renameProperties(config, propertyMappings)
}

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

/**
165 changes: 165 additions & 0 deletions test/unit/messaging/messaging.spec.ts
Original file line number Diff line number Diff line change
@@ -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?

Copy link
Author

Choose a reason for hiding this comment

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

Sure thing. I added the tests with valid payloads for the live activity start | update | end events

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

it('should throw given empty apns live activity token', () => {
expect(() => {
messaging.send({ apns: { liveActivityToken: '' }, topic: 'test' });
}).to.throw('apns.liveActivityToken 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)}`, () => {
@@ -2388,6 +2403,155 @@ describe('Messaging', () => {
},
},
},
{
label: 'APNS Start LiveActivity',
req: {
apns: {
liveActivityToken: 'live-activity-token',
headers:{
'apns-priority': '10'
},
payload: {
aps: {
timestamp: 1746475860808,
event: 'start',
'content-state': {
'demo': 1
},
'attributes-type': 'DemoAttributes',
'attributes': {
'demoAttribute': 1,
},
'alert': {
'title': 'test title',
'body': 'test body'
}
},
},
},
},
expectedReq: {
apns: {
live_activity_token: 'live-activity-token',
headers:{
'apns-priority': '10'
},
payload: {
aps: {
timestamp: 1746475860808,
event: 'start',
'content-state': {
'demo': 1
},
'attributes-type': 'DemoAttributes',
'attributes': {
'demoAttribute': 1,
},
'alert': {
'title': 'test title',
'body': 'test body'
}
},
},
},
},
},
{
label: 'APNS Update LiveActivity',
req: {
apns: {
liveActivityToken: 'live-activity-token',
headers:{
'apns-priority': '10'
},
payload: {
aps: {
timestamp: 1746475860808,
event: 'update',
'content-state': {
'test1': 100,
'test2': 'demo'
},
'alert': {
'title': 'test title',
'body': 'test body'
}
},
},
},
},
expectedReq: {
apns: {
live_activity_token: 'live-activity-token',
headers:{
'apns-priority': '10'
},
payload: {
aps: {
timestamp: 1746475860808,
event: 'update',
'content-state': {
'test1': 100,
'test2': 'demo'
},
'alert': {
'title': 'test title',
'body': 'test body'
}
},
},
},
},
},
{
label: 'APNS End LiveActivity',
req: {
apns: {
liveActivityToken: 'live-activity-token',
'headers':{
'apns-priority': '10'
},
payload: {
aps: {
timestamp: 1746475860808,
'dismissal-date': 1746475860808 + 60,
event: 'end',
'content-state': {
'test1': 100,
'test2': 'demo'
},
'alert': {
'title': 'test title',
'body': 'test body'
}
},
},
},
},
expectedReq: {
apns: {
live_activity_token: 'live-activity-token',
'headers':{
'apns-priority': '10'
},
payload: {
aps: {
timestamp: 1746475860808,
'dismissal-date': 1746475860808 + 60,
event: 'end',
'content-state': {
'test1': 100,
'test2': 'demo'
},
'alert': {
'title': 'test title',
'body': 'test body'
}
},
},
},
},
},
];

validMessages.forEach((config) => {
@@ -2404,6 +2568,7 @@ describe('Messaging', () => {
.then(() => {
const expectedReq = config.expectedReq || config.req;
expectedReq.token = 'mock-token';

expect(httpsRequestStub).to.have.been.calledOnce.and.calledWith({
method: 'POST',
data: { message: expectedReq },