GithubHelp home page GithubHelp logo

isabella232 / apns2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from flipboard/apns2

0.0 0.0 0.0 151 KB

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

JavaScript 100.00%

apns2's Introduction

APNS2

wercker status npm version Twitter

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

Now uses the native http2 module in Node.js v8.10 or later


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 } = require('apns2')

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

try {
  await client.send(bn)
} catch(err) {
  console.error(err.reason)
}

Send a basic notification with message and options:

const { BasicNotification } = require('apns2')

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

try {
  await client.send(bn)
} catch(err) {
  console.error(err.reason)
}

Silent

Send a silent notification using content-available key:

const { SilentNotification } = require('apns2')

let sn = new SilentNotification(deviceToken)

try {
  await client.send(sn)
} 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.

Many

Send multiple notifications concurrently:

const { BasicNotification } = require('apns2')

let notifications = [
  new BasicNotification(deviceToken1, 'Hello, World'),
  new BasicNotification(deviceToken2, 'Hello, World')
]

try {
  await client.sendMany(notifications)
} catch(err) {
  console.error(err.reason)
}

Advanced

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

const { Notification } = require('apns2')

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

try {
  await client.send(notification)
} 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 } = require('apns2')

// 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'
  ...
})

Requirements

apns2 requires Node.js v8.10 or later

apns2's People

Contributors

chrisrecalis avatar tyrone-sudeium avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.