Skip to content
Merged
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Unreleased

-
- [added] Added the `mutableContent` optional field to the `Aps` type of
the FCM API.
- [added] Added the support for specifying arbitrary custom key-value
fields in the `Aps` type.

# v5.11.0

Expand Down
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,10 @@ declare namespace admin.messaging {
badge?: number;
sound?: string;
contentAvailable?: boolean;
mutableContent?: boolean;
category?: string;
threadId?: string;
[customData: string]: any;
};

type ApsAlert = {
Expand Down
23 changes: 21 additions & 2 deletions src/messaging/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ export interface Aps {
contentAvailable?: boolean;
category?: string;
threadId?: string;
mutableContent?: boolean;
[customData: string]: any;
}

export interface ApsAlert {
Expand Down Expand Up @@ -274,17 +276,34 @@ function validateAps(aps: Aps) {

const propertyMappings = {
contentAvailable: 'content-available',
mutableContent: 'mutable-content',
threadId: 'thread-id',
};
Object.keys(propertyMappings).forEach((key) => {
if (key in aps && propertyMappings[key] in aps) {
throw new FirebaseMessagingError(
MessagingClientErrorCode.INVALID_PAYLOAD, `Multiple specifications for ${key} in Aps`);
}
});
renameProperties(aps, propertyMappings);

if (typeof aps['content-available'] !== 'undefined') {
if (aps['content-available'] === true) {
const contentAvailable = aps['content-available'];
if (typeof contentAvailable !== 'undefined' && contentAvailable !== 1) {
if (contentAvailable === true) {
aps['content-available'] = 1;
} else {
delete aps['content-available'];
}
}

const mutableContent = aps['mutable-content'];
if (typeof mutableContent !== 'undefined' && mutableContent !== 1) {
if (mutableContent === true) {
aps['mutable-content'] = 1;
} else {
delete aps['mutable-content'];
}
}
}

/**
Expand Down
75 changes: 75 additions & 0 deletions test/unit/messaging/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,18 @@ describe('Messaging', () => {
}).to.throw('apns.payload.aps must be a non-null object');
});
});
it(`should throw given APNS payload with duplicate fields`, () => {
expect(() => {
messaging.send({
apns: {
payload: {
aps: {'mutableContent': true, 'mutable-content': 1},
},
},
token: 'token',
});
}).to.throw('Multiple specifications for mutableContent in Aps');
});

const invalidApnsAlerts: any = [null, [], true, 1.23];
invalidApnsAlerts.forEach((alert) => {
Expand Down Expand Up @@ -2272,6 +2284,7 @@ describe('Messaging', () => {
sound: 'test.sound',
category: 'test.category',
contentAvailable: true,
mutableContent: true,
threadId: 'thread.id',
},
customKey1: 'custom.value',
Expand All @@ -2298,6 +2311,7 @@ describe('Messaging', () => {
'sound': 'test.sound',
'category': 'test.category',
'content-available': 1,
'mutable-content': 1,
'thread-id': 'thread.id',
},
customKey1: 'custom.value',
Expand Down Expand Up @@ -2325,6 +2339,67 @@ describe('Messaging', () => {
},
},
},
{
label: 'APNS content-available set explicitly',
req: {
apns: {
payload: {
aps: {
'content-available': 1,
},
},
},
},
expectedReq: {
apns: {
payload: {
aps: {'content-available': 1},
},
},
},
},
{
label: 'APNS mutableContent explicitly false',
req: {
apns: {
payload: {
aps: {
mutableContent: false,
},
},
},
},
expectedReq: {
apns: {
payload: {
aps: {},
},
},
},
},
{
label: 'APNS custom fields',
req: {
apns: {
payload: {
aps: {
k1: 'v1',
k2: true,
},
},
},
},
expectedReq: {
apns: {
payload: {
aps: {
k1: 'v1',
k2: true,
},
},
},
},
},
];

validMessages.forEach((config) => {
Expand Down