Skip to content
forked from AndrewBarba/apns2

Node client for connecting to Apple's Push Notification Service using the new HTTP/2 protocol with JSON web tokens

Notifications You must be signed in to change notification settings

marcopolee/apns2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

APNS2

npm version Twitter

Node client for connecting to Apple's Push Notification Service using the new HTTP/2 protocol with JSON web tokens.


3.0.0 betas can be tracked here. These versions are using the http2 library built into Node core

Create Client

Create an APNS client using a signing key:

const APNS = require('apns2');

let client = new APNS({
  team: `TFLP87PW54`,
  keyId: `123ABC456`,
  signingKey: fs.readFileSync(`${__dirname}/path/to/auth.p8`),
  defaultTopic: `com.tablelist.Tablelist`
});

Sending Notifications

Basic

Send a basic notification with message:

const { BasicNotification } = APNS;

let bn = new BasicNotification(deviceToken, 'Hello, World');

client.send(bn).then(() => {
  // sent successfully
}).catch(err => {
  console.error(err.reason);
});

Send a basic notification with message and options:

const { BasicNotification } = APNS;

let bn = new BasicNotification(deviceToken, 'Hello, World', {
  badge: 4,
  data: {
    userId: user.getUserId
  }
});

client.send(bn).then(() => {
  // sent successfully
}).catch(err => {
  console.error(err.reason);
});

Silent

Send a silent notification using content-available key:

const { SilentNotification } = APNS;

let sn = new SilentNotification(deviceToken);

client.send(sn).then(() => {
  // sent successfully
}).catch(err => {
  console.error(err.reason);
});

Note: Apple recommends 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

For complete control over the push notification packet use the base Notification class:

const { Notification } = APNS;

let notification = new Notification(deviceToken, {
  aps: { ... }
});

client.send(notification).then(() => {
  // sent successfully
}).catch(err => {
  console.error(err.reason);
});

Available options can be found at APNS Payload Options

Error Handling

All errors are defined in ./lib/errors.js and come directly from APNS Table 8-6

You can easily listen for these errors by attaching an error handler to the APNS client:

const errors = APNS.errors;

// Listen for a specific error
client.on(errors.badDeviceToken, err => {
  // Handle accordingly...
  // Perhaps delete token from your database
  console.error(err.reason, err.statusCode, err.notification.deviceToken);
});

// Listen for any error
client.on(errors.error, err => {
  console.error(err.reason, err.statusCode, err.notification.deviceToken);
});

Environments

By default the APNS client connects to the production push notification server. This is identical to passing in the options:

let client = new APNS({
  host: 'api.push.apple.com',
  port: 443,
  ...
});

To connect to the development push notification server, pass the options:

let client = new APNS({
  host: 'api.development.push.apple.com',
  port: 443,
  ...
});

Requirements

Node.js v6 or later.

About

Node client for connecting to Apple's Push Notification Service using the new HTTP/2 protocol with JSON web tokens

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 98.9%
  • Makefile 1.1%