Skip to content

feat: Add contentChanged in ApnsPayload #2934

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 4 commits 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 @@ -309,6 +309,7 @@ export interface MessagingOptions {
priority?: string;
restrictedPackageName?: string;
timeToLive?: number;
contentChanged?: boolean
}

// @public
Expand Down
18 changes: 18 additions & 0 deletions src/messaging/messaging-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ export interface Aps {
*/
mutableContent?: boolean;

/**
* Specifies whether to set the `content-changed` property on the message
* so the clients can modify the notification via app extensions.
*/
contentChanged?: boolean;

/**
* Type of the notification.
*/
Expand Down Expand Up @@ -948,6 +954,18 @@ export interface MessagingOptions {
*/
contentAvailable?: boolean;

/**
* On iOS, use this field to represent `content-changed` in the APNs payload.
*
* See {@link https://developer.apple.com/documentation/widgetkit/updating-widgets-with-widgetkit-push-notifications |
* Updating Widgets with WidgetKit Push Notifications} for more information.
*
* On Android and web, this field is ignored.
*
* **Default value:** `false`
*/
contentChanged?: boolean;

/**
* The package name of the application which the registration tokens must match
* in order to receive the message.
Expand Down
10 changes: 10 additions & 0 deletions src/messaging/messaging-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ function validateAps(aps: Aps): void {
contentAvailable: 'content-available',
mutableContent: 'mutable-content',
threadId: 'thread-id',
contentChanged: 'content-changed',
};
Object.keys(propertyMappings).forEach((key) => {
if (key in aps && propertyMappings[key] in aps) {
Expand All @@ -300,6 +301,15 @@ function validateAps(aps: Aps): void {
delete aps['mutable-content'];
}
}

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

function validateApsSound(sound: string | CriticalSound | undefined): void {
Expand Down
40 changes: 40 additions & 0 deletions test/unit/messaging/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2192,6 +2192,7 @@ describe('Messaging', () => {
category: 'test.category',
contentAvailable: true,
mutableContent: true,
contentChanged: true,
threadId: 'thread.id',
},
customKey1: 'custom.value',
Expand Down Expand Up @@ -2229,6 +2230,7 @@ describe('Messaging', () => {
'category': 'test.category',
'content-available': 1,
'mutable-content': 1,
'content-changed': 1,
'thread-id': 'thread.id',
},
customKey1: 'custom.value',
Expand Down Expand Up @@ -2380,6 +2382,44 @@ describe('Messaging', () => {
},
},
},
{
label: 'APNS contentChanged explicitly false',
req: {
apns: {
payload: {
aps: {
contentChanged: false,
},
},
},
},
expectedReq: {
apns: {
payload: {
aps: {},
},
},
},
},
{
label: 'APNS contentChanged set explicitly',
req: {
apns: {
payload: {
aps: {
'content-changed': 1,
},
},
},
},
expectedReq: {
apns: {
payload: {
aps: { 'content-changed': 1 },
},
},
},
},
{
label: 'APNS custom fields',
req: {
Expand Down
Loading