Skip to content

Commit

Permalink
Merge pull request #8 from bloom/android-fcm-update
Browse files Browse the repository at this point in the history
  • Loading branch information
nsakaimbo authored Aug 22, 2024
2 parents 088b865 + 8af59d9 commit 400e623
Show file tree
Hide file tree
Showing 31 changed files with 5,895 additions and 1,998 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 18.x, 20.x]
node-version: [14.x, 16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ test/logs
coverage
.sonar
release.sh
.nyc_output
.nyc_output
114 changes: 111 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ A node.js module for interfacing with Apple Push Notification, Google Cloud Mess
- [Usage](#usage)
- [GCM](#gcm)
- [APN](#apn)
- [FCM](#fcm)
- [WNS](#wns)
- [ADM](#adm)
- [Web-Push](#web-push)
Expand Down Expand Up @@ -64,6 +65,11 @@ const settings = {
production: false // true for APN production environment, false for APN sandbox environment,
...
},
fcm: {
appName: 'localFcmAppName',
serviceAccountKey: require('../firebase-project-service-account-key.json'), // firebase service-account-file.json,
credential: null // 'firebase-admin' Credential interface
},
adm: {
client_id: null,
client_secret: null,
Expand All @@ -86,13 +92,16 @@ const settings = {
contentEncoding: 'aes128gcm',
headers: {}
},
isAlwaysUseFCM: false, // true all messages will be sent through node-gcm (which actually uses FCM)
isAlwaysUseFCM: false, // true all messages will be sent through gcm/fcm api
isLegacyGCM: false // if true gcm messages will be sent through node-gcm (deprecated api), if false gcm messages will be sent through 'firebase-admin' lib
};

const push = new PushNotifications(settings);
```

- GCM options: see [node-gcm](https://github.com/ToothlessGear/node-gcm#custom-gcm-request-options)
- APN options: see [node-apn](https://github.com/node-apn/node-apn/blob/master/doc/provider.markdown)
- FCM options: see [firebase-admin](https://firebase.google.com/docs/admin/setup) (read [FCM](#fcm) section below!)
- ADM options: see [node-adm](https://github.com/umano/node-adm)
- WNS options: see [wns](https://github.com/tjanczuk/wns)
- Web-push options: see [web-push](https://github.com/web-push-libs/web-push)
Expand Down Expand Up @@ -356,7 +365,7 @@ The following parameters are used to create a GCM message. See https://developer
body_loc_args: data.locArgs, // Android, iOS
title_loc_key: data.titleLocKey, // Android, iOS
title_loc_args: data.titleLocArgs, // Android, iOS
android_channel_id: data.android_channel_id, // Android
android_channel_id: data.android_channel_id, // Android
},
}
```
Expand Down Expand Up @@ -486,7 +495,8 @@ The following parameters are used to create an APN message:
collapseId: data.collapseKey,
mutableContent: data.mutableContent || 0,
threadId: data.threadId,
pushType: data.pushType
pushType: data.pushType,
rawPayload: data.rawPayload
}
```
Expand All @@ -496,6 +506,7 @@ _data is the parameter in `push.send(registrationIds, data)`_
- **Please note** that `topic` is required ([see node-apn docs](https://github.com/node-apn/node-apn/blob/master/doc/notification.markdown#notificationtopic)). When using token-based authentication, specify the bundle ID of the app.
When using certificate-based authentication, the topic is usually your app's bundle ID.
More details can be found under https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns
- `rawPayload` (hidden 'node-apn' lib notification param) [source code](https://github.com/node-apn/node-apn/blob/master/lib/notification/index.js#L99) this param will replace all payload
### Silent push notifications
Expand All @@ -519,6 +530,103 @@ const silentPushData = {
}
```
## FCM
The following parameters are used to create an FCM message (Android/APN):
[node-gcm](https://github.com/ToothlessGear/node-gcm) lib for `GCM` method use old firebase api (will be [deprecated ](https://firebase.google.com/docs/cloud-messaging/migrate-v1?hl=en&authuser=0))
Settings:
- `settings.fcm.appName` [firebase app name](https://firebase.google.com/docs/reference/admin/node/firebase-admin.app.app#appname) (required)
- `settings.fcm.serviceAccountKey` [firebase service account file](https://firebase.google.com/docs/admin/setup#initialize_the_sdk_in_non-google_environments) use downloaded 'service-account-file.json'
- `settings.fcm.credential` [firebase credential](https://firebase.google.com/docs/reference/admin/node/firebase-admin.app.credential)
Note: one of `serviceAccountKey`, `credential` fcm options is required
```js
const tokens = [
'e..Gwso:APA91.......7r910HljzGUVS_f...kbyIFk2sK6......D2s6XZWn2E21x',
];

const notifications = {
collapseKey: Math.random().toString().replace('0.', ''),
priority: 'high',
sound: 'default',
title: 'Title 1',
body: 'Body 2',
// titleLocKey: 'GREETING',
// titleLocArgs: ['Smith', 'M'],
// fcm_notification: {
// title: 'Title 1',
// body: 'Body 2',
// sound: 'default',
// default_vibrate_timings: true,
// },
// alert: {
// title: 'Title 2',
// body: 'Body 2'
// },
custom: {
frined_id: 54657,
list_id: 'N7jSif1INyZkA7r910HljzGUVS',
},
};

pushNotifications.send(tokens, notifications, (error, result) => {
if (error) {
console.log('[error]', error);
throw error;
} else {
console.log('[result]', result, result.at(0));
}
});
```
`fcm_notification` - object that will be passed to
```js
new gcm.Message({ ..., notification: data.fcm_notification })
```
Fcm object that will be sent to provider ([Fcm message format](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?authuser=0#Message)) :
```json
{
"data": {
"frined_id": "54657",
"list_id": "N7jSif1INyZkA7r910HljzGUVS"
},
"android": {
"collapse_key": "5658586678087056",
"priority": "high",
"notification": {
"title": "Title 1",
"body": "Body 2",
"sound": "default"
},
"ttl": 2419200000
},
"apns": {
"headers": {
"apns-expiration": "1697456586",
"apns-collapse-id": "5658586678087056"
},
"payload": {
"aps": {
"sound": "default",
"alert": {
"title": "Title 1",
"body": "Body 2"
}
}
}
},
"tokens": [
"e..Gwso:APA91.......7r910HljzGUVS_f...kbyIFk2sK6......D2s6XZWn2E21x"
]
}
```
## WNS
The following fields are used to create a WNS message:
Expand Down
15 changes: 12 additions & 3 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ module.exports = {
// 4 weeks in seconds (https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json)
APN_METHOD: 'apn',
GCM_METHOD: 'gcm',
FCM_METHOD: 'fcm',
ADM_METHOD: 'adm',
WNS_METHOD: 'wns',
WEB_METHOD: 'webPush',
UNKNOWN_METHOD: 'unknown',
DEFAULT_SETTINGS: {
gcm: {
id: null // PUT YOUR GCM SERVER API KEY,

},
fcm: {
appName: null,
serviceAccountKey: null,
credential: null
},
apn: {
// See options at https://github.com/node-apn/node-apn/blob/master/doc/provider.markdown
Expand Down Expand Up @@ -48,6 +55,7 @@ module.exports = {
client_id: null,
// PUT YOUR ADM CLIENT ID,
client_secret: null // PUT YOUR ADM CLIENT SECRET,

},
wns: {
client_id: null,
Expand All @@ -63,12 +71,13 @@ module.exports = {
subject: "< 'mailto' Address or URL >",
publicKey: '< URL Safe Base64 Encoded Public Key >',
privateKey: '< URL Safe Base64 Encoded Private Key >'
}
// gcmAPIKey: '< GCM API Key >',
} // gcmAPIKey: '< GCM API Key >',
// TTL: 2419200
// headers: { }
// contentEncoding: '< Encoding type, e.g.: aesgcm or aes128gcm >'

},
isAlwaysUseFCM: false
isAlwaysUseFCM: false,
isLegacyGCM: false
}
};
Loading

0 comments on commit 400e623

Please sign in to comment.