From 7741187d0a3385c2b01ed264f0d946bc0c778f93 Mon Sep 17 00:00:00 2001 From: Hiranya Jayathilaka Date: Thu, 22 Mar 2018 16:50:09 -0700 Subject: [PATCH 1/2] Supporting APNS mutable-content option --- src/index.d.ts | 2 + src/messaging/messaging.ts | 23 +++++++- test/unit/messaging/messaging.spec.ts | 75 +++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 1bacf55fe8..863f22ca46 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -405,8 +405,10 @@ declare namespace admin.messaging { badge?: number; sound?: string; contentAvailable?: boolean; + mutableContent?: boolean; category?: string; threadId?: string; + [customField: string]: any; }; type ApsAlert = { diff --git a/src/messaging/messaging.ts b/src/messaging/messaging.ts index fa4e93ba0b..d415842e75 100644 --- a/src/messaging/messaging.ts +++ b/src/messaging/messaging.ts @@ -152,6 +152,8 @@ export interface Aps { contentAvailable?: boolean; category?: string; threadId?: string; + mutableContent?: boolean; + [customField: string]: any; } export interface ApsAlert { @@ -274,17 +276,34 @@ function validateAps(aps: Aps) { const propertyMappings = { contentAvailable: 'content-available', + mutableContent: 'mutable-content', threadId: 'thread-id', }; + Object.keys(propertyMappings).forEach((key) => { + if (key in aps && propertyMappings[key] in aps) { + throw new FirebaseMessagingError( + MessagingClientErrorCode.INVALID_PAYLOAD, `Multiple specifications for ${key} in Aps`); + } + }); renameProperties(aps, propertyMappings); - if (typeof aps['content-available'] !== 'undefined') { - if (aps['content-available'] === true) { + const contentAvailable = aps['content-available']; + if (typeof contentAvailable !== 'undefined' && contentAvailable !== 1) { + if (contentAvailable === true) { aps['content-available'] = 1; } else { delete aps['content-available']; } } + + const mutableContent = aps['mutable-content']; + if (typeof mutableContent !== 'undefined' && mutableContent !== 1) { + if (mutableContent === true) { + aps['mutable-content'] = 1; + } else { + delete aps['mutable-content']; + } + } } /** diff --git a/test/unit/messaging/messaging.spec.ts b/test/unit/messaging/messaging.spec.ts index b848897695..fd5b6c46f7 100644 --- a/test/unit/messaging/messaging.spec.ts +++ b/test/unit/messaging/messaging.spec.ts @@ -1827,6 +1827,18 @@ describe('Messaging', () => { }).to.throw('apns.payload.aps must be a non-null object'); }); }); + it(`should throw given APNS payload with duplicate fields`, () => { + expect(() => { + messaging.send({ + apns: { + payload: { + aps: {'mutableContent': true, 'mutable-content': 1}, + }, + }, + token: 'token', + }); + }).to.throw('Multiple specifications for mutableContent in Aps'); + }); const invalidApnsAlerts: any = [null, [], true, 1.23]; invalidApnsAlerts.forEach((alert) => { @@ -2252,6 +2264,7 @@ describe('Messaging', () => { sound: 'test.sound', category: 'test.category', contentAvailable: true, + mutableContent: true, threadId: 'thread.id', }, customKey1: 'custom.value', @@ -2278,6 +2291,7 @@ describe('Messaging', () => { 'sound': 'test.sound', 'category': 'test.category', 'content-available': 1, + 'mutable-content': 1, 'thread-id': 'thread.id', }, customKey1: 'custom.value', @@ -2305,6 +2319,67 @@ describe('Messaging', () => { }, }, }, + { + label: 'APNS content-available set explicitly', + req: { + apns: { + payload: { + aps: { + 'content-available': 1, + }, + }, + }, + }, + expectedReq: { + apns: { + payload: { + aps: {'content-available': 1}, + }, + }, + }, + }, + { + label: 'APNS mutableContent explicitly false', + req: { + apns: { + payload: { + aps: { + mutableContent: false, + }, + }, + }, + }, + expectedReq: { + apns: { + payload: { + aps: {}, + }, + }, + }, + }, + { + label: 'APNS custom fields', + req: { + apns: { + payload: { + aps: { + k1: 'v1', + k2: true, + }, + }, + }, + }, + expectedReq: { + apns: { + payload: { + aps: { + k1: 'v1', + k2: true, + }, + }, + }, + }, + }, ]; validMessages.forEach((config) => { From daa32f66f2cb60be3f7da4282ab3d16eed440bad Mon Sep 17 00:00:00 2001 From: Hiranya Jayathilaka Date: Mon, 2 Apr 2018 14:21:59 -0700 Subject: [PATCH 2/2] Renamed customFields to customData; Updated CHANGELOG --- CHANGELOG.md | 5 ++++- src/index.d.ts | 2 +- src/messaging/messaging.ts | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3295ec378f..0e8d5c7b46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ # Unreleased -- +- [added] Added the `mutableContent` optional field to the `Aps` type of + the FCM API. +- [added] Added the support for specifying arbitrary custom key-value + fields in the `Aps` type. # v5.11.0 diff --git a/src/index.d.ts b/src/index.d.ts index 863f22ca46..ffa79b6292 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -408,7 +408,7 @@ declare namespace admin.messaging { mutableContent?: boolean; category?: string; threadId?: string; - [customField: string]: any; + [customData: string]: any; }; type ApsAlert = { diff --git a/src/messaging/messaging.ts b/src/messaging/messaging.ts index d415842e75..c112857aa5 100644 --- a/src/messaging/messaging.ts +++ b/src/messaging/messaging.ts @@ -153,7 +153,7 @@ export interface Aps { category?: string; threadId?: string; mutableContent?: boolean; - [customField: string]: any; + [customData: string]: any; } export interface ApsAlert {