diff --git a/CHANGELOG.md b/CHANGELOG.md index 42f464d765..6da584d58f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Unreleased -- +- [added] `messaging.ApsAlert` type now supports subtitle in its payload. # v6.1.0 diff --git a/src/index.d.ts b/src/index.d.ts index 87e097092c..8c30e32d6f 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -426,11 +426,14 @@ declare namespace admin.messaging { type ApsAlert = { title?: string; + subtitle?: string; body?: string; locKey?: string; locArgs?: string[]; titleLocKey?: string; titleLocArgs?: string[]; + subtitleLocKey?: string; + subtitleLocArgs?: string[]; actionLocKey?: string; launchImage?: string; }; diff --git a/src/messaging/messaging.ts b/src/messaging/messaging.ts index eef402ddab..fbcf17cdad 100644 --- a/src/messaging/messaging.ts +++ b/src/messaging/messaging.ts @@ -180,11 +180,14 @@ export interface Aps { export interface ApsAlert { title?: string; + subtitle?: string; body?: string; locKey?: string; locArgs?: string[]; titleLocKey?: string; titleLocArgs?: string[]; + subtitleLocKey?: string; + subtitleLocArgs?: string[]; actionLocKey?: string; launchImage?: string; } @@ -357,12 +360,20 @@ function validateApsAlert(alert: string | ApsAlert) { MessagingClientErrorCode.INVALID_PAYLOAD, 'apns.payload.aps.alert.titleLocKey is required when specifying titleLocArgs'); } + if (validator.isNonEmptyArray(apsAlert.subtitleLocArgs) && + !validator.isNonEmptyString(apsAlert.subtitleLocKey)) { + throw new FirebaseMessagingError( + MessagingClientErrorCode.INVALID_PAYLOAD, + 'apns.payload.aps.alert.subtitleLocKey is required when specifying subtitleLocArgs'); + } const propertyMappings = { locKey: 'loc-key', locArgs: 'loc-args', titleLocKey: 'title-loc-key', titleLocArgs: 'title-loc-args', + subtitleLocKey: 'subtitle-loc-key', + subtitleLocArgs: 'subtitle-loc-args', actionLocKey: 'action-loc-key', launchImage: 'launch-image', }; diff --git a/test/unit/messaging/messaging.spec.ts b/test/unit/messaging/messaging.spec.ts index f01986b0b7..648dad3a4b 100644 --- a/test/unit/messaging/messaging.spec.ts +++ b/test/unit/messaging/messaging.spec.ts @@ -1733,6 +1733,24 @@ describe('Messaging', () => { }).to.throw('titleLocKey is required when specifying titleLocArgs'); }); + it('should throw given apns subtitleLocArgs without subtitleLocKey', () => { + const message: Message = { + condition: 'topic-name', + apns: { + payload: { + aps: { + alert: { + subtitleLocArgs: ['foo'], + }, + }, + }, + }, + }; + expect(() => { + messaging.send(message); + }).to.throw('subtitleLocKey is required when specifying subtitleLocArgs'); + }); + it('should throw given apns locArgs without locKey', () => { const message: Message = { condition: 'topic-name', @@ -2260,11 +2278,17 @@ describe('Messaging', () => { payload: { aps: { alert: { + title: 'title', + subtitle: 'subtitle', + body: 'body', titleLocKey: 'title.loc.key', titleLocArgs: ['arg1', 'arg2'], + subtitleLocKey: 'subtitle.loc.key', + subtitleLocArgs: ['arg1', 'arg2'], locKey: 'body.loc.key', locArgs: ['arg1', 'arg2'], actionLocKey: 'action.loc.key', + launchImage: 'image', }, badge: 42, sound: 'test.sound', @@ -2287,11 +2311,17 @@ describe('Messaging', () => { payload: { aps: { 'alert': { + 'title': 'title', + 'subtitle': 'subtitle', + 'body': 'body', 'title-loc-key': 'title.loc.key', 'title-loc-args': ['arg1', 'arg2'], + 'subtitle-loc-key': 'subtitle.loc.key', + 'subtitle-loc-args': ['arg1', 'arg2'], 'loc-key': 'body.loc.key', 'loc-args': ['arg1', 'arg2'], 'action-loc-key': 'action.loc.key', + 'launch-image': 'image', }, 'badge': 42, 'sound': 'test.sound',