Skip to content

Commit 70e6b6f

Browse files
authored
feat: Add Firebase Cloud Messaging (FCM) HTTP/2 support and option fcmEnableLegacyHttpTransport to use legacy HTTP/1.1 (#274)
1 parent a15decd commit 70e6b6f

File tree

5 files changed

+88
-25
lines changed

5 files changed

+88
-25
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ The official Push Notification adapter for Parse Server. See [Parse Server Push
1919
- [Configure Parse Server](#configure-parse-server)
2020
- [Apple Push Options](#apple-push-options)
2121
- [Android Push Options](#android-push-options)
22+
- [Firebase Cloud Messaging (FCM)](#firebase-cloud-messaging-fcm)
2223
- [Google Cloud Service Account Key](#google-cloud-service-account-key)
2324
- [Migration to FCM HTTP v1 API (June 2024)](#migration-to-fcm-http-v1-api-june-2024)
25+
- [HTTP/1.1 Legacy Option](#http11-legacy-option)
2426
- [Expo Push Options](#expo-push-options)
2527
- [Bundled with Parse Server](#bundled-with-parse-server)
2628
- [Logging](#logging)
@@ -110,6 +112,10 @@ android: {
110112
}
111113
```
112114

115+
### Firebase Cloud Messaging (FCM)
116+
117+
This section contains some considerations when using FCM, regardless of the destination ecosystems the push notification is sent to.
118+
113119
#### Google Cloud Service Account Key
114120

115121
The Firebase console allows to easily create and download a Google Cloud service account key JSON file with the required permissions. Instead of setting `firebaseServiceAccount` to the path of the JSON file, you can provide an object representing a Google Cloud service account key:
@@ -139,6 +145,19 @@ android: {
139145
}
140146
```
141147

148+
#### HTTP/1.1 Legacy Option
149+
150+
With the introduction of the FCM HTTP v1 API, support for HTTP/2 was added which provides faster throughput for push notifications. To use the older version HTTP/1.1 set `fcmEnableLegacyHttpTransport: true` in your push options.
151+
152+
Example options:
153+
154+
```js
155+
android: {
156+
firebaseServiceAccount: __dirname + '/firebase.json',
157+
fcmEnableLegacyHttpTransport: true
158+
}
159+
```
160+
142161
### Expo Push Options
143162

144163
Example options:

package-lock.json

+19-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@parse/node-apn": "6.0.1",
2828
"@parse/node-gcm": "1.0.2",
2929
"expo-server-sdk": "3.10.0",
30-
"firebase-admin": "12.2.0",
30+
"firebase-admin": "12.3.0",
3131
"npmlog": "7.0.1",
3232
"parse": "5.2.0",
3333
"web-push": "3.6.7"

spec/FCM.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from 'path';
22
import log from 'npmlog';
33
import FCM from '../src/FCM.js';
4+
import { getApps, deleteApp } from 'firebase-admin/app';
45

56
const testArgs = {
67
firebaseServiceAccount: path.join(
@@ -13,6 +14,10 @@ const testArgs = {
1314
};
1415

1516
describe('FCM', () => {
17+
beforeEach(async () => {
18+
getApps().forEach(app => deleteApp(app));
19+
});
20+
1621
it('can initialize', () => {
1722
const fcm = new FCM(testArgs);
1823
expect(fcm).toBeDefined();
@@ -31,6 +36,37 @@ describe('FCM', () => {
3136
expect(spy).toHaveBeenCalledWith('parse-server-push-adapter FCM', 'invalid push payload');
3237
});
3338

39+
it('initializes with fcmEnableLegacyHttpTransport set to false by default', () => {
40+
const fcm = new FCM(testArgs);
41+
expect(fcm).toBeDefined();
42+
expect(fcm.sender).toBeDefined();
43+
expect(fcm.sender.useLegacyTransport).toEqual(false);
44+
});
45+
46+
it('can initialize with fcmEnableLegacyHttpTransport set to false', () => {
47+
const legacyHttpTransportArgs = {
48+
...testArgs,
49+
fcmEnableLegacyHttpTransport: false
50+
};
51+
52+
const fcm = new FCM(legacyHttpTransportArgs);
53+
expect(fcm).toBeDefined();
54+
expect(fcm.sender).toBeDefined();
55+
expect(fcm.sender.useLegacyTransport).toEqual(false);
56+
});
57+
58+
it('can initialize with fcmEnableLegacyHttpTransport set to true', () => {
59+
const legacyHttpTransportArgs = {
60+
...testArgs,
61+
fcmEnableLegacyHttpTransport: true
62+
};
63+
64+
const fcm = new FCM(legacyHttpTransportArgs);
65+
expect(fcm).toBeDefined();
66+
expect(fcm.sender).toBeDefined();
67+
expect(fcm.sender.useLegacyTransport).toEqual(true);
68+
});
69+
3470
it('can send successful FCM android request', async () => {
3571
const spyVerbose = spyOn(log, 'verbose').and.callFake(() => {});
3672
const spyInfo = spyOn(log, 'info').and.callFake(() => {});

src/FCM.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,26 @@ export default function FCM(args, pushType) {
2525
);
2626
}
2727

28+
const fcmEnableLegacyHttpTransport = typeof args.fcmEnableLegacyHttpTransport === 'boolean'
29+
? args.fcmEnableLegacyHttpTransport
30+
: false;
31+
2832
let app;
2933
if (getApps().length === 0) {
3034
app = initializeApp({ credential: cert(args.firebaseServiceAccount) });
3135
} else {
3236
app = getApp();
3337
}
38+
3439
this.sender = getMessaging(app);
35-
this.pushType = pushType; // Push type is only used to remain backwards compatible with APNS and GCM
40+
41+
if (fcmEnableLegacyHttpTransport) {
42+
this.sender.enableLegacyHttpTransport();
43+
log.warn(LOG_PREFIX, 'Legacy HTTP/1.1 transport is enabled. This is a deprecated feature and support for this flag will be removed in the future.');
44+
}
45+
46+
// Push type is only used to remain backwards compatible with APNS and GCM
47+
this.pushType = pushType;
3648
}
3749

3850
FCM.FCMRegistrationTokensMax = FCMRegistrationTokensMax;

0 commit comments

Comments
 (0)