Skip to content

Commit

Permalink
Merge pull request #128 from appfeel/dev/silent
Browse files Browse the repository at this point in the history
Add new configuration property "silent"
  • Loading branch information
alex-friedl committed Mar 5, 2020
2 parents 9b0a2f6 + b720280 commit 066a39c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,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
/*
* 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 @@ -366,9 +367,9 @@ The following parameters are used to create an APN message:
priority: data.priority === 'normal' ? 5 : 10,
encoding: data.encoding,
payload: data.custom || {},
badge: data.badge,
sound: data.sound,
alert: data.alert || {
badge: data.silent === true ? undefined : data.badge,
badge: data.sound === true ? undefined : data.sound,
alert: data.sound === true ? undefined : data.alert || {
title: data.title,
body: data.body,
'title-loc-key': data.titleLocKey,
Expand Down Expand Up @@ -402,13 +403,17 @@ _data is the parameter in `push.send(registrationIds, data)`_
iOS supports silent push notifications which are not displayed to the user but only used to transmit data.
You can send silent push notifications by sending a push notification with normal priority and no sound, badge or alert.
Silent push notifications must not include sound, badge or alert and have normal priority.
By setting the `silent` property to `true` the values for `sound`, `badge` and `alert` will be overridden to `undefined`.
Priority will be overridden to `normal`.
```js
const silentPushData = {
topic: 'yourTopic',
contentAvailable: true,
priority: 'normal',
silent: true,
payload: {
yourKey: 'yourValue',
...
Expand Down
47 changes: 33 additions & 14 deletions src/sendAPN.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ const extractExpiry = R.cond([
[R.T, () => expiryFromTtl(DEFAULT_TTL)],
]);

const getPropValueOrUndefinedIfIsSilent = (propName, data) =>
R.ifElse(
R.propEq('silent', true),
R.always(undefined),
R.prop(propName)
)(data);

const getDefaultAlert = data => ({
title: data.title,
body: data.body,
'title-loc-key': data.titleLocKey,
'title-loc-args': data.titleLocArgs,
'loc-key': data.locKey,
// bodyLocArgs is kept for backward compatibility
'loc-args': data.locArgs || data.bodyLocArgs,
'launch-image': data.launchImage,
action: data.action,
});

const pushDataWithDefaultAlert = data =>
R.when(
R.propSatisfies(R.isNil, 'alert'),
R.assoc('alert', getDefaultAlert(data)),
data
);

class APN {
constructor(settings) {
try {
Expand All @@ -33,22 +59,15 @@ class APN {
const message = new apn.Notification({
retryLimit: data.retries || -1,
expiry: extractExpiry(data),
priority: data.priority === 'normal' ? 5 : 10,
priority: data.priority === 'normal' || data.silent === true ? 5 : 10,
encoding: data.encoding,
payload: data.custom || {},
badge: data.badge,
sound: data.sound,
alert: data.alert || {
title: data.title,
body: data.body,
'title-loc-key': data.titleLocKey,
'title-loc-args': data.titleLocArgs,
'loc-key': data.locKey,
// bodyLocArgs is kept for backward compatibility
'loc-args': data.locArgs || data.bodyLocArgs,
'launch-image': data.launchImage,
action: data.action,
},
badge: getPropValueOrUndefinedIfIsSilent('badge', data),
sound: getPropValueOrUndefinedIfIsSilent('sound', data),
alert: getPropValueOrUndefinedIfIsSilent(
'alert',
pushDataWithDefaultAlert(data)
),
topic: data.topic,
category: data.category || data.clickAction,
contentAvailable: data.contentAvailable,
Expand Down
9 changes: 6 additions & 3 deletions test/send/sendAPN.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ describe('push-notifications-apn', () => {
_regIds.forEach(regId => expect(regIds).to.include(regId));
expect(message).to.be.instanceOf(apn.Notification);
expect(message.aps.sound).to.be.undefined();
expect(message.aps.alert.title).to.be.undefined();
expect(message.aps.alert.body).to.be.undefined();
expect(message.aps.alert).to.be.undefined();
expect(message.aps.badge).to.be.undefined();
expect(message.topic).to.equal('testTopic');
expect(message.priority).to.equal(5);
Expand All @@ -204,7 +203,11 @@ describe('push-notifications-apn', () => {
const silentPushData = {
topic: 'testTopic',
contentAvailable: true,
priority: 'normal',
priority: 'high',
silent: true,
alert: { title: 'ignore' },
badge: 'ignore',
sound: 'ignore',
custom: {
testKey: 'testValue',
},
Expand Down

0 comments on commit 066a39c

Please sign in to comment.