From 4b9da74dbca7ebca3a870275364df7129ed016fe Mon Sep 17 00:00:00 2001 From: Kai Wu Date: Tue, 25 Oct 2022 12:20:50 -0700 Subject: [PATCH] Expose 'icon' field from the Firebase Messaging SDK (#6722) * Expose 'icon' field from the Firebase Messaging SDK as part of the 'notification' payload. Fixes for b/241885017 * Update API reports * Update lovely-swans-shake.md * Ran Format.ts --- .changeset/lovely-swans-shake.md | 5 +++++ common/api-review/messaging-sw.api.md | 1 + common/api-review/messaging.api.md | 1 + .../src/helpers/externalizePayload.test.ts | 14 +++++++++++--- .../messaging/src/helpers/externalizePayload.ts | 5 +++++ .../src/interfaces/internal-message-payload.ts | 1 + packages/messaging/src/interfaces/public-types.ts | 6 ++++++ 7 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 .changeset/lovely-swans-shake.md diff --git a/.changeset/lovely-swans-shake.md b/.changeset/lovely-swans-shake.md new file mode 100644 index 00000000000..d8ec5595685 --- /dev/null +++ b/.changeset/lovely-swans-shake.md @@ -0,0 +1,5 @@ +--- +'@firebase/messaging': minor +--- + +Expose 'icon' field from the Firebase Messaging SDK as part of the 'notification' payload diff --git a/common/api-review/messaging-sw.api.md b/common/api-review/messaging-sw.api.md index 88150fd0cc5..74f823de196 100644 --- a/common/api-review/messaging-sw.api.md +++ b/common/api-review/messaging-sw.api.md @@ -52,6 +52,7 @@ export { NextFn } // @public export interface NotificationPayload { body?: string; + icon?: string; image?: string; title?: string; } diff --git a/common/api-review/messaging.api.md b/common/api-review/messaging.api.md index e48cecb6871..b74fc3a69fa 100644 --- a/common/api-review/messaging.api.md +++ b/common/api-review/messaging.api.md @@ -55,6 +55,7 @@ export { NextFn } // @public export interface NotificationPayload { body?: string; + icon?: string; image?: string; title?: string; } diff --git a/packages/messaging/src/helpers/externalizePayload.test.ts b/packages/messaging/src/helpers/externalizePayload.test.ts index f800a3323eb..0ff2924d67d 100644 --- a/packages/messaging/src/helpers/externalizePayload.test.ts +++ b/packages/messaging/src/helpers/externalizePayload.test.ts @@ -27,6 +27,7 @@ describe('externalizePayload', () => { title: 'title', body: 'body', image: 'image', + icon: 'icon', // eslint-disable-next-line camelcase click_action: 'https://www.self_orgin.com' }, @@ -38,7 +39,12 @@ describe('externalizePayload', () => { }; const payload: MessagePayload = { - notification: { title: 'title', body: 'body', image: 'image' }, + notification: { + title: 'title', + body: 'body', + image: 'image', + icon: 'icon' + }, from: 'from', collapseKey: 'collapse', messageId: 'mid', @@ -77,7 +83,8 @@ describe('externalizePayload', () => { notification: { title: 'title', body: 'body', - image: 'image' + image: 'image', + icon: 'icon' }, data: { foo: 'foo', @@ -100,7 +107,8 @@ describe('externalizePayload', () => { notification: { title: 'title', body: 'body', - image: 'image' + image: 'image', + icon: 'icon' }, data: { foo: 'foo', diff --git a/packages/messaging/src/helpers/externalizePayload.ts b/packages/messaging/src/helpers/externalizePayload.ts index 6553fa8c631..c3bcbd0d419 100644 --- a/packages/messaging/src/helpers/externalizePayload.ts +++ b/packages/messaging/src/helpers/externalizePayload.ts @@ -60,6 +60,11 @@ function propagateNotificationPayload( if (!!image) { payload.notification!.image = image; } + + const icon = messagePayloadInternal.notification!.icon; + if (!!icon) { + payload.notification!.icon = icon; + } } function propagateDataPayload( diff --git a/packages/messaging/src/interfaces/internal-message-payload.ts b/packages/messaging/src/interfaces/internal-message-payload.ts index 868d141ae9b..ddf8911087f 100644 --- a/packages/messaging/src/interfaces/internal-message-payload.ts +++ b/packages/messaging/src/interfaces/internal-message-payload.ts @@ -38,6 +38,7 @@ export interface NotificationPayloadInternal extends NotificationOptions { // See:https://firebase.google.com/docs/cloud-messaging/xmpp-server-ref. // eslint-disable-next-line camelcase click_action?: string; + icon?: string; } // Defined in diff --git a/packages/messaging/src/interfaces/public-types.ts b/packages/messaging/src/interfaces/public-types.ts index 382db13a640..71087aadfe0 100644 --- a/packages/messaging/src/interfaces/public-types.ts +++ b/packages/messaging/src/interfaces/public-types.ts @@ -38,6 +38,12 @@ export interface NotificationPayload { * The URL of an image that is downloaded on the device and displayed in the notification. */ image?: string; + + /** + * The URL to use for the notification's icon. If you don't send this key in the request, + * FCM displays the launcher icon specified in your app manifest. + */ + icon?: string; } /**