Skip to content

add subtitle in messaging.ApsAlert payload #393

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

Merged
merged 3 commits into from
Nov 27, 2018
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Unreleased

-
- [added] `messaging.ApsAlert` type now supports subtitle in its payload.

# v6.1.0

Expand Down
3 changes: 3 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
11 changes: 11 additions & 0 deletions src/messaging/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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',
};
Expand Down
30 changes: 30 additions & 0 deletions test/unit/messaging/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand Down