Skip to content

Commit

Permalink
cleanup silent notification
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewBarba committed Jun 22, 2016
1 parent d1b3453 commit b245814
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
16 changes: 1 addition & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,7 @@ client.send(sn).then(() => {
});
```

Send a silent notification with options:

```javascript
const SilentNotification = APNS.SilentNotification;

let sn = new SilentNotification(deviceToken, {
badge: getUnreadNotificationCount()
});

client.send(sn).then(() => {
// sent successfully
}).catch(err => {
console.error(err.reason);
});
```
Note: [Apple recommends](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1) that no options other than the `content-available` flag be sent in order for a notification to truly be silent and wake up your app in the background. Therefore this class does not accept any additional options in the constructor.

#### Advanced

Expand Down
5 changes: 2 additions & 3 deletions lib/notifications/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class Notification {
* @param {String} [options.sound]
* @param {String} [options.category]
* @param {Object} [options.data]
* @param {Number} [options.content-available]
* @param {Boolean} [options.silent] - Identical to `content-available`: 1
* @param {Boolean} [options.contentAvailable]
* @param {Number} [options.priority]
* @param {Object} [options.aps] - override all setters
*/
Expand Down Expand Up @@ -76,7 +75,7 @@ class Notification {
}

// Check for "silent" notification
if (this._options[`content-available`] || this._options.silent) {
if (_.isBoolean(this._options.contentAvailable)) {
result.aps[`content-available`] = 1;
}

Expand Down
10 changes: 5 additions & 5 deletions lib/notifications/silent-notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class SilentNotification extends Notification {
* @param {String} deviceToken
* @param {Object} [options] - see super class
*/
constructor(deviceToken, options) {
options = options || {};
options.priority = priority.throttled;
options.silent = true;
super(deviceToken, options);
constructor(deviceToken) {
super(deviceToken, {
contentAvailable: true,
priority: priority.throttled
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apns2",
"version": "1.0.0",
"version": "1.1.0",
"description": "Node client for connecting to Apple's Push Notification Service using the new HTTP/2 protocol with JSON web tokens.",
"author": "Andrew Barba <[email protected]>",
"main": "lib/apns.js",
Expand Down
28 changes: 28 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,39 @@ describe('apns', () => {
return apns.send(basicNotification);
});

it('should send a basic notification with options', () => {
let basicNotification = new APNS.BasicNotification(deviceToken, `Hello, 1`, {
badge: 1
});
return apns.send(basicNotification);
});

it('should send a basic notification with additional data', () => {
let basicNotification = new APNS.BasicNotification(deviceToken, `Hello, ICON`, {
badge: 0,
data: {
url: `venue/icon`
}
});
return apns.send(basicNotification);
});

it('should send a silent notification', () => {
let silentNotification = new APNS.SilentNotification(deviceToken);
return apns.send(silentNotification);
});

it('should send a notification', () => {
let notification = new APNS.Notification(deviceToken, {
aps: {
alert: {
body: `Hello, Tablelist`
}
}
});
return apns.send(notification);
});

it('should send both notifications', () => {
let basicNotification = new APNS.BasicNotification(deviceToken, `Hello, Multiple`);
let silentNotification = new APNS.SilentNotification(deviceToken);
Expand Down

0 comments on commit b245814

Please sign in to comment.