Skip to content

Commit e99c07a

Browse files
authored
Merge pull request #230 from appfeel/dev/bump-firebase-remove-gcm
Remove legacy GCM API support and bump Firebase Admin SDK to v13
2 parents 1934e02 + c78d671 commit e99c07a

20 files changed

+1201
-934
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"@babel/preset-env",
55
{
66
"targets": {
7-
"node": "14"
7+
"node": "18"
88
}
99
}
1010
]

.github/copilot-instructions.md

Lines changed: 162 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## Project Overview
44

5-
This repository implements a Node.js module for sending push notifications across multiple platforms: Apple (APN), Google (GCM/FCM), Windows (WNS), Amazon (ADM), and Web-Push. The core logic is in `lib/` and `src/`, with each platform handled by a dedicated file (e.g., `sendAPN.js`, `sendFCM.js`).
5+
This repository implements a Node.js module for sending push notifications across multiple platforms: Apple (APN), Google (FCM), Windows (WNS), Amazon (ADM), and Web-Push. The core logic is in `lib/` and `src/`, with each platform handled by a dedicated file (e.g., `sendAPN.js`, `sendFCM.js`).
6+
7+
**Note:** Legacy GCM (Google Cloud Messaging) support has been removed. All Android push notifications now route exclusively through Firebase Cloud Messaging (FCM) using the Firebase Admin SDK.
68

79
## Architecture & Data Flow
810

@@ -14,40 +16,187 @@ This repository implements a Node.js module for sending push notifications acros
1416

1517
## Developer Workflows
1618

17-
- **Install:** `npm install`
18-
- **Test:** Run all tests with `npm test`. Tests are in `test/` and cover basic flows and platform-specific cases.
19+
- **Install:** `npm install` (requires Node.js 18+)
20+
- **Test:** Run all tests with `npm test`. Tests are in `test/` and cover basic flows and platform-specific cases (87 tests, all passing).
1921
- **Debug:** Use the callback or Promise error/result from `push.send`. Each result object includes method, success/failure counts, and error details per device.
2022
- **Build:** No build step required for basic usage. ES6 is used, but compatible with ES5 via Babel if needed.
2123

2224
## Conventions & Patterns
2325

24-
- **Platform-specific files:** Each push service has its own file for isolation and clarity.
26+
- **Platform-specific files:** Each push service has its own file for isolation and clarity. Legacy GCM is no longer supported.
2527
- **Unified Data Model:** The `data` object for notifications is normalized across platforms. See `README.md` for all supported fields.
2628
- **Error Handling:** Errors are unified and returned in the result array from `push.send`.
2729
- **RegId Format:** Prefer object format for registration IDs (`{id, type}`), but string format is supported for legacy reasons.
28-
- **Chunking:** Android tokens are chunked in batches of 1,000 automatically.
29-
- **Constants:** Use constants from `constants.js` for platform types.
30+
- **Android Routing:** All Android push notifications route through FCM using Firebase Admin SDK.
31+
- **Chunking:** Android tokens are chunked in batches of 1,000 automatically by FCM.
32+
- **Constants:** Use constants from `constants.js` for platform types. Available constants: `FCM_METHOD`, `APN_METHOD`, `WNS_METHOD`, `ADM_METHOD`, `WEB_METHOD`, `UNKNOWN_METHOD`.
33+
34+
## Firebase Cloud Messaging (FCM) - Modern Implementation
35+
36+
### Message Building (src/utils/tools.js)
37+
38+
**buildAndroidMessage(data, options)**
39+
- Converts unified notification data to Firebase Admin SDK AndroidMessage format
40+
- Returns plain JavaScript object (no wrapper functions)
41+
- Properties mapped to camelCase (Firebase SDK standard)
42+
- Removes undefined values for clean API calls
43+
- Converts TTL from seconds to milliseconds
44+
- Supports all 20+ AndroidNotification properties
45+
46+
**buildAndroidNotification(data)**
47+
- Maps input `data` object to AndroidNotification interface
48+
- Supported properties:
49+
- Basic: `title`, `body`, `icon`, `color`, `sound`, `tag`, `imageUrl`
50+
- Localization: `titleLocKey`, `titleLocArgs`, `bodyLocKey`, `bodyLocArgs`
51+
- Android-specific: `channelId`, `notificationCount`, `ticker`, `sticky`, `visibility`
52+
- Behavior: `clickAction`, `priority`, `localOnly`, `eventTimestamp`
53+
- Accessibility: `ticker`
54+
- Vibration: `vibrateTimingsMillis`, `defaultVibrateTimings`
55+
- Sound: `defaultSound`
56+
- LED: `lightSettings`, `defaultLightSettings`
57+
- Proxy: `proxy` (notification-level, values: 'allow', 'deny', 'if_priority_lowered')
58+
59+
### FCM Configuration (src/sendFCM.js)
60+
61+
**Initialization Options:**
62+
- `credential` or `serviceAccountKey` (required) - Firebase authentication
63+
- `projectId` (optional) - Explicit Google Cloud project ID
64+
- `databaseURL` (optional) - Realtime Database URL
65+
- `storageBucket` (optional) - Cloud Storage bucket
66+
- `serviceAccountId` (optional) - Service account email
67+
- `databaseAuthVariableOverride` (optional) - Auth override for RTDB rules
68+
- `httpAgent` (optional) - HTTP proxy agent for network requests
69+
- `httpsAgent` (optional) - HTTPS proxy agent for network requests
70+
71+
All optional properties are dynamically added to Firebase initialization if defined.
72+
73+
### Proxy Support
74+
75+
**Two levels of proxy configuration:**
76+
77+
1. **Network-level (SDK initialization)**
78+
- `settings.fcm.httpAgent` and `settings.fcm.httpsAgent`
79+
- Controls how Firebase Admin SDK communicates with Google servers
80+
- Uses proxy agent libraries (http-proxy-agent, https-proxy-agent)
81+
- Applied at app initialization
82+
83+
2. **Notification-level (Android device)**
84+
- `data.proxy` property in notification message
85+
- Controls how Android devices handle notifications in proxy scenarios
86+
- Values: 'allow', 'deny', 'if_priority_lowered'
87+
- Per-message configuration
88+
89+
### Message Format
90+
91+
Firebase Admin SDK expects:
92+
```javascript
93+
{
94+
tokens: [...],
95+
android: {
96+
collapseKey: string,
97+
priority: 'high' | 'normal',
98+
ttl: number (milliseconds),
99+
restrictedPackageName: string,
100+
directBootOk: boolean,
101+
data: { [key: string]: string },
102+
notification: { ...AndroidNotification properties... },
103+
fcmOptions: { analyticsLabel: string }
104+
},
105+
apns: { ...APNs payload... }
106+
}
107+
```
30108

31109
## Integration Points
32110

33111
- **External Libraries:**
34-
- APN: `node-apn`
35-
- FCM: `firebase-admin`
36-
- GCM: `node-gcm`
112+
- APN: `@parse/node-apn`
113+
- FCM: `firebase-admin` (all Android push notifications)
37114
- ADM: `node-adm`
38115
- WNS: `wns`
39116
- Web-Push: `web-push`
117+
- Proxy: `http-proxy-agent`, `https-proxy-agent` (user-supplied)
118+
- Note: Legacy `node-gcm` library has been removed
40119
- **Credentials:** Place service account keys and certificates in appropriate locations (see `README.md` for examples).
41120

42121
## Key Files & Directories
43122

44-
- `lib/` and `src/`: Main implementation (mirrored structure)
45-
- `test/`: Test cases and sample credentials
46-
- `README.md`: Usage, configuration, and data model reference
123+
- `lib/` and `src/`: Main implementation (mirrored structure, both CommonJS)
124+
- `lib/sendFCM.js` / `src/sendFCM.js`: Firebase Admin SDK integration
125+
- `lib/utils/tools.js` / `src/utils/tools.js`: Message building utilities
126+
- `lib/utils/fcmMessage.js` / `src/utils/fcmMessage.js`: FCM message formatting
127+
- `test/send/sendFCM.js`: FCM test suite (10 test cases covering message format, proxy support, and Firebase AppOptions)
128+
- `test/`: Test cases (87 tests total, all passing)
129+
- `README.md`: Complete usage guide, configuration examples, and property documentation
130+
- `.github/copilot-instructions.md`: This file - AI agent guidance
131+
132+
## Recent Changes - Legacy GCM Removal & Firebase Admin SDK Alignment
133+
134+
### What Changed
135+
136+
**Removed:**
137+
- `buildGcmMessage()` function (wrapper pattern with toJson() method)
138+
- `buildGcmNotification()` function
139+
- Post-processing delete statements for undefined properties
140+
- References to legacy `node-gcm` library
141+
142+
**Added:**
143+
- `buildAndroidMessage()` - Direct Firebase Admin SDK compatible builder
144+
- `buildAndroidNotification()` - Proper Android notification interface
145+
- 15+ new Android notification properties (ticker, sticky, visibility, LED settings, etc.)
146+
- Support for all 9 Firebase Admin SDK AppOptions (projectId, databaseURL, storageBucket, etc.)
147+
- Proxy support at both SDK and notification levels
148+
- Comprehensive test coverage (10 FCM-specific tests)
149+
150+
### Migration Pattern
151+
152+
**Before (Legacy GCM):**
153+
```javascript
154+
const message = buildGcmMessage(data).toJson();
155+
// Result: wrapper object with toJson() method
156+
```
157+
158+
**After (Firebase Admin SDK):**
159+
```javascript
160+
const message = buildAndroidMessage(data);
161+
// Result: plain object directly compatible with firebase-admin
162+
```
163+
164+
### Property Naming
165+
166+
All properties now use **camelCase** (Firebase Admin SDK standard):
167+
- `android_channel_id``channelId`
168+
- `title_loc_key``titleLocKey`
169+
- `body_loc_key``bodyLocKey`
170+
- `body_loc_args``bodyLocArgs`
171+
- etc.
172+
173+
### Testing
174+
175+
New test suite added with 7 test cases:
176+
- `should accept projectId in settings`
177+
- `should accept databaseURL in settings`
178+
- `should accept storageBucket in settings`
179+
- `should accept serviceAccountId in settings`
180+
- `should accept databaseAuthVariableOverride in settings`
181+
- `should accept multiple Firebase AppOptions together`
182+
- Plus 3 existing proxy tests (httpAgent, httpsAgent, both)
183+
184+
All 87 tests passing, zero regressions.
47185

48186
## Example Usage
49187

50-
See `README.md` for a full example of settings, registration ID formats, and sending notifications.
188+
See `README.md` for complete examples including:
189+
- FCM settings configuration with all AppOptions
190+
- Notification data with all supported properties
191+
- Network proxy agent setup
192+
- Notification-level proxy configuration
193+
194+
## Future Considerations
195+
196+
- Monitor Firebase Admin SDK updates for new AndroidMessage properties
197+
- Consider typed interfaces for better TypeScript support
198+
- Track Android notification API changes and feature additions
199+
- Evaluate performance of dynamic property filtering approach
51200

52201
---
53202

0 commit comments

Comments
 (0)