From c79cdc7bc326a65323b7527a9d0583aa406a616b Mon Sep 17 00:00:00 2001 From: Cyrille Hemidy Date: Sat, 20 Jul 2019 12:37:10 +0200 Subject: [PATCH 1/5] add analytics_label --- src/messaging/messaging-types.ts | 15 +++++++++++++++ test/unit/messaging/messaging.spec.ts | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/messaging/messaging-types.ts b/src/messaging/messaging-types.ts index a8a3cac5b4..176be17e1f 100644 --- a/src/messaging/messaging-types.ts +++ b/src/messaging/messaging-types.ts @@ -27,6 +27,7 @@ interface BaseMessage { android?: AndroidConfig; webpush?: WebpushConfig; apns?: ApnsConfig; + fcmOptions?: FcmOptions; } interface TokenMessage extends BaseMessage { @@ -60,6 +61,10 @@ export interface Notification { body?: string; } +export interface FcmOptions { + analyticsLabel?: string; +} + export interface WebpushConfig { headers?: {[key: string]: string}; data?: {[key: string]: string}; @@ -97,6 +102,7 @@ export interface WebpushNotification { export interface ApnsConfig { headers?: {[key: string]: string}; payload?: ApnsPayload; + fcmOptions?: ApnsFcmOptions; } export interface ApnsPayload { @@ -135,6 +141,10 @@ export interface ApsAlert { launchImage?: string; } +export interface ApnsFcmOptions { + analyticsLabel?: string; +} + export interface AndroidConfig { collapseKey?: string; priority?: ('high' | 'normal'); @@ -142,6 +152,7 @@ export interface AndroidConfig { restrictedPackageName?: string; data?: {[key: string]: string}; notification?: AndroidNotification; + fcmOptions?: AndroidFcmOptions; } export interface AndroidNotification { @@ -159,6 +170,10 @@ export interface AndroidNotification { channelId?: string; } +export interface AndroidFcmOptions { + analyticsLabel?: string; +} + /* Payload for data messages */ export interface DataMessagePayload { [key: string]: string; diff --git a/test/unit/messaging/messaging.spec.ts b/test/unit/messaging/messaging.spec.ts index 7479dbb583..626080fcf0 100644 --- a/test/unit/messaging/messaging.spec.ts +++ b/test/unit/messaging/messaging.spec.ts @@ -2750,6 +2750,14 @@ describe('Messaging', () => { }, }, }, + { + label: 'Generic fcmOptions message', + req: { + fcmOptions: { + analyticsLabel: 'test.analytics', + }, + }, + }, { label: 'Android data message', req: { @@ -2852,6 +2860,9 @@ describe('Messaging', () => { bodyLocArgs: ['arg1', 'arg2'], channelId: 'test.channel', }, + fcmOptions: { + analyticsLabel: 'test.analytics', + }, }, }, expectedReq: { @@ -2878,6 +2889,9 @@ describe('Messaging', () => { body_loc_args: ['arg1', 'arg2'], channel_id: 'test.channel', }, + fcmOptions: { + analyticsLabel: 'test.analytics', + }, }, }, }, @@ -2999,6 +3013,9 @@ describe('Messaging', () => { customKey1: 'custom.value', customKey2: {nested: 'value'}, }, + fcmOptions: { + analyticsLabel: 'test.analytics', + }, }, }, expectedReq: { @@ -3032,6 +3049,9 @@ describe('Messaging', () => { customKey1: 'custom.value', customKey2: {nested: 'value'}, }, + fcmOptions: { + analyticsLabel: 'test.analytics', + }, }, }, }, From 124d695e705461e0f8f82f474ddf6b0f0b1b52f8 Mon Sep 17 00:00:00 2001 From: Cyrille Hemidy Date: Sat, 20 Jul 2019 13:03:39 +0200 Subject: [PATCH 2/5] update doc --- src/index.d.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/index.d.ts b/src/index.d.ts index 50df1a0e29..37b3c8afe4 100755 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -3554,6 +3554,7 @@ type BaseMessage = { android?: admin.messaging.AndroidConfig; webpush?: admin.messaging.WebpushConfig; apns?: admin.messaging.ApnsConfig; + fcmOptions?: admin.messaging.FcmOptions; }; interface TokenMessage extends BaseMessage { @@ -3616,6 +3617,11 @@ declare namespace admin.messaging { * Android notification to be included in the message. */ notification?: AndroidNotification; + + /** + * Options for features provided by the FCM SDK for Android. + */ + fcmOptions?: AndroidFcmOptions; } /** @@ -3701,6 +3707,17 @@ declare namespace admin.messaging { channelId?: string; } + /** + * Represents Options for features provided by the FCM SDK for Android. + */ + interface AndroidFcmOptions { + + /** + * The Label associated with the message's analytics data.. + */ + analyticsLabel?: string; + } + /** * Represents the APNs-specific options that can be included in an * {@link admin.messaging.Message}. Refer to @@ -3718,6 +3735,11 @@ declare namespace admin.messaging { * An APNs payload to be included in the message. */ payload?: ApnsPayload; + + /** + * Options for features provided by the FCM SDK for iOS. + */ + fcmOptions?: ApnsFcmOptions; } /** * Represents the payload of an APNs message. Mainly consists of the `aps` @@ -3816,6 +3838,28 @@ declare namespace admin.messaging { volume?: number; } + /** + * Represents Options for features provided by the FCM SDK for iOS.. + */ + interface ApnsFcmOptions { + + /** + * The Label associated with the message's analytics data.. + */ + analyticsLabel?: string; + } + + /** + * Represents Platform independent options for features provided by the FCM SDKs. + */ + interface FcmOptions { + + /** + * The Label associated with the message's analytics data.. + */ + analyticsLabel?: string; + } + /** * A notification that can be included in {@link admin.messaging.Message}. From aede523fca32a8483711fb0c168fbb9694e011df Mon Sep 17 00:00:00 2001 From: Cyrille Hemidy Date: Tue, 23 Jul 2019 10:14:55 +0200 Subject: [PATCH 3/5] nit fix --- src/index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 37b3c8afe4..597fa94890 100755 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -3708,12 +3708,12 @@ declare namespace admin.messaging { } /** - * Represents Options for features provided by the FCM SDK for Android. + * Represents options for features provided by the FCM SDK for Android. */ interface AndroidFcmOptions { /** - * The Label associated with the message's analytics data.. + * The label associated with the message's analytics data. */ analyticsLabel?: string; } @@ -3839,23 +3839,23 @@ declare namespace admin.messaging { } /** - * Represents Options for features provided by the FCM SDK for iOS.. + * Represents options for features provided by the FCM SDK for iOS. */ interface ApnsFcmOptions { /** - * The Label associated with the message's analytics data.. + * The label associated with the message's analytics data. */ analyticsLabel?: string; } /** - * Represents Platform independent options for features provided by the FCM SDKs. + * Represents platform-independent options for features provided by the FCM SDKs. */ interface FcmOptions { /** - * The Label associated with the message's analytics data.. + * The label associated with the message's analytics data. */ analyticsLabel?: string; } From e73f798b61194ff14fc9d6e1c8aaaaf433efb755 Mon Sep 17 00:00:00 2001 From: Cyrille Hemidy Date: Wed, 24 Jul 2019 09:52:43 +0200 Subject: [PATCH 4/5] add validators --- src/messaging/messaging-types.ts | 62 ++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/src/messaging/messaging-types.ts b/src/messaging/messaging-types.ts index 176be17e1f..c6958cefe9 100644 --- a/src/messaging/messaging-types.ts +++ b/src/messaging/messaging-types.ts @@ -306,6 +306,7 @@ export function validateMessage(message: Message) { validateAndroidConfig(message.android); validateWebpushConfig(message.webpush); validateApnsConfig(message.apns); + validateFcmOptions(message.fcmOptions); } /** @@ -360,8 +361,49 @@ function validateApnsConfig(config: ApnsConfig) { } validateStringMap(config.headers, 'apns.headers'); validateApnsPayload(config.payload); + validateApnsFcmOptions(config.fcmOptions); } +/** + * Checks if the given ApnsFcmOptions object is valid. + * + * @param {ApnsFcmOptions} fcmOptions An object to be validated. + */ +function validateApnsFcmOptions(fcmOptions: ApnsFcmOptions) { + if (typeof fcmOptions === 'undefined') { + return; + } else if (!validator.isNonNullObject(fcmOptions)) { + throw new FirebaseMessagingError( + MessagingClientErrorCode.INVALID_PAYLOAD, 'fcmOptions must be a non-null object'); + } + + if (typeof fcmOptions.analyticsLabel !== 'undefined' && !validator.isString(fcmOptions.analyticsLabel)) { + throw new FirebaseMessagingError( + MessagingClientErrorCode.INVALID_PAYLOAD, 'analyticsLabel must be a string value'); + } +} + +/** + * Checks if the given FcmOptions object is valid. + * + * @param {FcmOptions} fcmOptions An object to be validated. + */ +function validateFcmOptions(fcmOptions: FcmOptions) { + if (typeof fcmOptions === 'undefined') { + return; + } else if (!validator.isNonNullObject(fcmOptions)) { + throw new FirebaseMessagingError( + MessagingClientErrorCode.INVALID_PAYLOAD, 'fcmOptions must be a non-null object'); + } + + if (typeof fcmOptions.analyticsLabel !== 'undefined' && !validator.isString(fcmOptions.analyticsLabel)) { + throw new FirebaseMessagingError( + MessagingClientErrorCode.INVALID_PAYLOAD, 'analyticsLabel must be a string value'); + } +} + + + /** * Checks if the given ApnsPayload object is valid. The object must have a valid aps value. * @@ -550,6 +592,7 @@ function validateAndroidConfig(config: AndroidConfig) { } validateStringMap(config.data, 'android.data'); validateAndroidNotification(config.notification); + validateAndroidFcmOptions(config.fcmOptions); const propertyMappings = { collapseKey: 'collapse_key', @@ -600,3 +643,22 @@ function validateAndroidNotification(notification: AndroidNotification) { }; renameProperties(notification, propertyMappings); } + +/** + * Checks if the given AndroidFcmOptions object is valid. + * + * @param {AndroidFcmOptions} fcmOptions An object to be validated. + */ +function validateAndroidFcmOptions(fcmOptions: AndroidFcmOptions) { + if (typeof fcmOptions === 'undefined') { + return; + } else if (!validator.isNonNullObject(fcmOptions)) { + throw new FirebaseMessagingError( + MessagingClientErrorCode.INVALID_PAYLOAD, 'fcmOptions must be a non-null object'); + } + + if (typeof fcmOptions.analyticsLabel !== 'undefined' && !validator.isString(fcmOptions.analyticsLabel)) { + throw new FirebaseMessagingError( + MessagingClientErrorCode.INVALID_PAYLOAD, 'analyticsLabel must be a string value'); + } +} From 0f5c0b178ea45e4102a2daed456d4463f9af65da Mon Sep 17 00:00:00 2001 From: Cyrille Hemidy Date: Fri, 26 Jul 2019 09:44:40 +0200 Subject: [PATCH 5/5] add test on fcmOptions --- test/unit/messaging/messaging.spec.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/unit/messaging/messaging.spec.ts b/test/unit/messaging/messaging.spec.ts index 626080fcf0..ef8ffa29b4 100644 --- a/test/unit/messaging/messaging.spec.ts +++ b/test/unit/messaging/messaging.spec.ts @@ -2469,6 +2469,24 @@ describe('Messaging', () => { messaging.send({data: arg, topic: 'test'}); }).to.throw('data must be a non-null object'); }); + + it(`should throw given invalid fcmOptions: ${JSON.stringify(arg)}`, () => { + expect(() => { + messaging.send({fcmOptions: arg, topic: 'test'}); + }).to.throw('fcmOptions must be a non-null object'); + }); + + it(`should throw given invalid AndroidFcmOptions: ${JSON.stringify(arg)}`, () => { + expect(() => { + messaging.send({android: {fcmOptions: arg}, topic: 'test'}); + }).to.throw('fcmOptions must be a non-null object'); + }); + + it(`should throw given invalid ApnsFcmOptions: ${JSON.stringify(arg)}`, () => { + expect(() => { + messaging.send({apns: {fcmOptions: arg}, topic: 'test'}); + }).to.throw('fcmOptions must be a non-null object'); + }); }); const invalidDataMessages: any[] = [