Skip to content

Commit

Permalink
Merge cd448dd into 42bbb44
Browse files Browse the repository at this point in the history
  • Loading branch information
cjroth committed Dec 23, 2020
2 parents 42bbb44 + cd448dd commit 8abf1b3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ const data = {
body: 'body'
// details: https://github.com/node-apn/node-apn/blob/master/doc/notification.markdown#convenience-setters
},
silent: false, // apn, will override badge, sound, alert and priority if set to true
silent: false, // gcm, apn, will override badge, sound, alert and priority if set to true on iOS, will omit `notification` property and send as data-only on Android/GCM
/*
* A string is also accepted as a payload for alert
* Your notification won't appear on ios if alert is empty object
Expand Down Expand Up @@ -350,6 +350,23 @@ In that way, they can be accessed in android in the following two ways:
title = title != null ? title : extras.getString("gcm.notification.title");
```
### Silent push notifications
GCM supports silent push notifications which are not displayed to the user but only used to transmit data.
```js
const silentPushData = {
topic: 'yourTopic',
silent: true,
custom: {
yourKey: 'yourValue',
...
}
}
```
Internally, `silent: true` will tell `node-gcm` _not_ to send the `notification` property and only send the `custom` property. If you don't specify `silent: true` then the push notifications will still be visible on the device. Note that this is nearly the same behavior as `phoneGap: true` and will set `content-available` to `true`.
### Send to custom recipients (device groups or topics)
In order to override the default behaviour of sending the notifications to a list of device tokens,
Expand Down
7 changes: 4 additions & 3 deletions src/sendGCM.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,15 @@ const sendGCM = (regIds, data, settings) => {
const message = new gcm.Message({
// See https://developers.google.com/cloud-messaging/http-server-ref#table5
collapseKey: data.collapseKey,
priority: data.priority === 'normal' ? data.priority : 'high',
contentAvailable: data.contentAvailable || false,
priority: data.priority === 'normal' || data.silent ? 'normal' : 'high',
contentAvailable: data.silent ? true : data.contentAvailable || false,
delayWhileIdle: data.delayWhileIdle || false,
timeToLive: extractTimeToLive(data),
restrictedPackageName: data.restrictedPackageName,
dryRun: data.dryRun || false,
data: opts.phonegap === true ? Object.assign(custom, notification) : custom, // See https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#android-behaviour
notification: opts.phonegap === true ? undefined : notification,
notification:
opts.phonegap === true || data.silent === true ? undefined : notification,
});
let chunk = 0;

Expand Down
5 changes: 3 additions & 2 deletions test/send/sendGCM.js
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ describe('push-notifications-gcm', () => {
);
expect(retries).to.be.a('number');
expect(message).to.be.instanceOf(gcm.Message);
expect(message.params.notification).to.be.undefined();
expect(message.params.priority).to.equal('normal');
expect(message.params.contentAvailable).to.be.true();
expect(message.params.data.testKey).to.eql('testValue');
Expand Down Expand Up @@ -688,8 +689,8 @@ describe('push-notifications-gcm', () => {

it('all responses should be successful (callback, custom data undefined)', (done) => {
const silentPushData = {
contentAvailable: true,
priority: 'normal',
priority: 'high',
silent: true,
custom: {
testKey: 'testValue',
},
Expand Down

0 comments on commit 8abf1b3

Please sign in to comment.