Skip to content

Commit a072536

Browse files
committed
re-do PR from original repo and fix tests
1 parent ef16496 commit a072536

20 files changed

+3362
-249
lines changed

README.md

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ A node.js module for interfacing with Apple Push Notification, Google Cloud Mess
1515
- [Features](#features)
1616
- [Usage](#usage)
1717
- [GCM](#gcm)
18+
- [FCM](#fcm)
1819
- [APN](#apn)
1920
- [WNS](#wns)
2021
- [ADM](#adm)
@@ -55,6 +56,11 @@ const settings = {
5556
phonegap: false, // phonegap compatibility mode, see below (defaults to false)
5657
...
5758
},
59+
fcm: {
60+
appName: 'localFcmAppName',
61+
serviceAccountKey: require('../firebase-project-service-account-key.json'), // firebase service-account-file.json,
62+
credential: null // 'firebase-admin' Credential interface
63+
},
5864
apn: {
5965
token: {
6066
key: './certs/key.p8', // optionally: fs.readFileSync('./certs/key.p8')
@@ -86,12 +92,15 @@ const settings = {
8692
contentEncoding: 'aes128gcm',
8793
headers: {}
8894
},
89-
isAlwaysUseFCM: false, // true all messages will be sent through node-gcm (which actually uses FCM)
95+
isAlwaysUseFCM: false, // true all messages will be sent through gcm/fcm api
96+
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
97+
9098
};
9199
const push = new PushNotifications(settings);
92100
```
93101

94102
- GCM options: see [node-gcm](https://github.com/ToothlessGear/node-gcm#custom-gcm-request-options)
103+
- FCM options: see [firebase-admin](https://firebase.google.com/docs/admin/setup) (read [FCM](#fcm) section below!)
95104
- APN options: see [node-apn](https://github.com/node-apn/node-apn/blob/master/doc/provider.markdown)
96105
- ADM options: see [node-adm](https://github.com/umano/node-adm)
97106
- WNS options: see [wns](https://github.com/tjanczuk/wns)
@@ -486,7 +495,8 @@ The following parameters are used to create an APN message:
486495
collapseId: data.collapseKey,
487496
mutableContent: data.mutableContent || 0,
488497
threadId: data.threadId,
489-
pushType: data.pushType
498+
pushType: data.pushType,
499+
rawPayload: data.rawPayload
490500
}
491501
```
492502
@@ -496,6 +506,7 @@ _data is the parameter in `push.send(registrationIds, data)`_
496506
- **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.
497507
When using certificate-based authentication, the topic is usually your app's bundle ID.
498508
More details can be found under https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns
509+
- `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
499510
500511
### Silent push notifications
501512
@@ -519,6 +530,94 @@ const silentPushData = {
519530
}
520531
```
521532
533+
## FCM
534+
The following parameters are used to create an FCM message (Android/APN):
535+
[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))
536+
Settings:
537+
- `settings.fcm.appName` [firebase app name](https://firebase.google.com/docs/reference/admin/node/firebase-admin.app.app#appname) (required)
538+
- `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'
539+
- `settings.fcm.credential` [firebase credential](https://firebase.google.com/docs/reference/admin/node/firebase-admin.app.credential)
540+
Note: one of `serviceAccountKey`, `credential` fcm options is required
541+
```js
542+
const tokens = [
543+
'e..Gwso:APA91.......7r910HljzGUVS_f...kbyIFk2sK6......D2s6XZWn2E21x',
544+
];
545+
546+
const notifications = {
547+
collapseKey: Math.random().toString().replace('0.', ''),
548+
priority: 'high',
549+
sound: 'default',
550+
title: 'Title 1',
551+
body: 'Body 2',
552+
// titleLocKey: 'GREETING',
553+
// titleLocArgs: ['Smith', 'M'],
554+
// fcm_notification: {
555+
// title: 'Title 1',
556+
// body: 'Body 2',
557+
// sound: 'default',
558+
// default_vibrate_timings: true,
559+
// },
560+
// alert: {
561+
// title: 'Title 2',
562+
// body: 'Body 2'
563+
// },
564+
custom: {
565+
frined_id: 54657,
566+
list_id: 'N7jSif1INyZkA7r910HljzGUVS',
567+
},
568+
};
569+
570+
pushNotifications.send(tokens, notifications, (error, result) => {
571+
if (error) {
572+
console.log('[error]', error);
573+
throw error;
574+
} else {
575+
console.log('[result]', result, result.at(0));
576+
}
577+
});
578+
```
579+
`fcm_notification` - object that will be passed to
580+
```js
581+
new gcm.Message({ ..., notification: data.fcm_notification })
582+
```
583+
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)) :
584+
```json
585+
{
586+
"data": {
587+
"friend_id": "54657",
588+
"list_id": "N7jSif1INyZkA7r910HljzGUVS"
589+
},
590+
"android": {
591+
"collapse_key": "5658586678087056",
592+
"priority": "high",
593+
"notification": {
594+
"title": "Title 1",
595+
"body": "Body 2",
596+
"sound": "default"
597+
},
598+
"ttl": 2419200000
599+
},
600+
"apns": {
601+
"headers": {
602+
"apns-expiration": "1697456586",
603+
"apns-collapse-id": "5658586678087056"
604+
},
605+
"payload": {
606+
"aps": {
607+
"sound": "default",
608+
"alert": {
609+
"title": "Title 1",
610+
"body": "Body 2"
611+
}
612+
}
613+
}
614+
},
615+
"tokens": [
616+
"e..Gwso:APA91.......7r910HljzGUVS_f...kbyIFk2sK6......D2s6XZWn2E21x"
617+
]
618+
}
619+
```
620+
522621
## WNS
523622
524623
The following fields are used to create a WNS message:

0 commit comments

Comments
 (0)