Skip to content

Commit cf8b2fb

Browse files
Add mappings to image in notification (#653)
* Add mappings to image in notification - Add missing mappings for imageUrl to image in FCM notifications * Update unit tests
1 parent 8299d74 commit cf8b2fb

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/messaging/messaging-types.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,18 @@ function validateApnsFcmOptions(fcmOptions: ApnsFcmOptions) {
392392
throw new FirebaseMessagingError(
393393
MessagingClientErrorCode.INVALID_PAYLOAD, 'analyticsLabel must be a string value');
394394
}
395+
396+
const propertyMappings: {[key: string]: string} = {
397+
imageUrl: 'image',
398+
};
399+
Object.keys(propertyMappings).forEach((key) => {
400+
if (key in fcmOptions && propertyMappings[key] in fcmOptions) {
401+
throw new FirebaseMessagingError(
402+
MessagingClientErrorCode.INVALID_PAYLOAD,
403+
`Multiple specifications for ${key} in ApnsFcmOptions`);
404+
}
405+
});
406+
renameProperties(fcmOptions, propertyMappings);
395407
}
396408

397409
/**
@@ -430,6 +442,18 @@ function validateNotification(notification: Notification) {
430442
throw new FirebaseMessagingError(
431443
MessagingClientErrorCode.INVALID_PAYLOAD, 'notification.imageUrl must be a valid URL string');
432444
}
445+
446+
const propertyMappings: {[key: string]: string} = {
447+
imageUrl: 'image',
448+
};
449+
Object.keys(propertyMappings).forEach((key) => {
450+
if (key in notification && propertyMappings[key] in notification) {
451+
throw new FirebaseMessagingError(
452+
MessagingClientErrorCode.INVALID_PAYLOAD,
453+
`Multiple specifications for ${key} in Notification`);
454+
}
455+
});
456+
renameProperties(notification, propertyMappings);
433457
}
434458

435459
/**
@@ -674,6 +698,7 @@ function validateAndroidNotification(notification: AndroidNotification) {
674698
titleLocKey: 'title_loc_key',
675699
titleLocArgs: 'title_loc_args',
676700
channelId: 'channel_id',
701+
imageUrl: 'image',
677702
};
678703
renameProperties(notification, propertyMappings);
679704
}

test/unit/messaging/messaging.spec.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,6 +2801,13 @@ describe('Messaging', () => {
28012801
{
28022802
label: 'Generic notification message',
28032803
req: {
2804+
notification: {
2805+
title: 'test.title',
2806+
body: 'test.body',
2807+
imageUrl: 'https://example.com/image.png',
2808+
},
2809+
},
2810+
expectedReq: {
28042811
notification: {
28052812
title: 'test.title',
28062813
body: 'test.body',
@@ -2830,6 +2837,19 @@ describe('Messaging', () => {
28302837
{
28312838
label: 'Android notification message',
28322839
req: {
2840+
android: {
2841+
notification: {
2842+
title: 'test.title',
2843+
body: 'test.body',
2844+
icon: 'test.icon',
2845+
color: '#112233',
2846+
sound: 'test.sound',
2847+
tag: 'test.tag',
2848+
imageUrl: 'https://example.com/image.png',
2849+
},
2850+
},
2851+
},
2852+
expectedReq: {
28332853
android: {
28342854
notification: {
28352855
title: 'test.title',
@@ -2912,7 +2932,7 @@ describe('Messaging', () => {
29122932
color: '#112233',
29132933
sound: 'test.sound',
29142934
tag: 'test.tag',
2915-
image: 'https://example.com/image.png',
2935+
imageUrl: 'https://example.com/image.png',
29162936
clickAction: 'test.click.action',
29172937
titleLocKey: 'title.loc.key',
29182938
titleLocArgs: ['arg1', 'arg2'],
@@ -3076,7 +3096,7 @@ describe('Messaging', () => {
30763096
},
30773097
fcmOptions: {
30783098
analyticsLabel: 'test.analytics',
3079-
image: 'https://example.com/image.png',
3099+
imageUrl: 'https://example.com/image.png',
30803100
},
30813101
},
30823102
},

0 commit comments

Comments
 (0)