/apns2

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

Primary LanguageJavaScript

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 and environment flag:

const { APNS } = require('apns2')

let client = new APNS ({
    team: teamId,
    keyId: keyId,
    key: keyText,
    production: true
})

Sending Notifications

Send a basic notification with message:

let headers = {
    'apns-priority': 10,
    'apns-topic': topic
}

let payload = {
        aps: {
            alert: 'Testing node',
            badge: 1,
            sound: 'default'
        }
}

// array of tokens
let tokens = [goodDeviceToken, invalidToken]

//client sends the same payload with same headers to multiple tokens
//authorisation is the only thing done internally
client.connect().then(() => {
    client.push({
        deviceTokens: tokens,
        payload: payload,
        headers: headers
    }).then(results => console.log('done', results)).catch(err => console.log('can not push', err))
}).catch(err => {
    console.log('can not connect', err)
})

Available options can be found at APNS Payload Options

Error Handling

Just iterate trough results (token is added to the actual server result)

Requirements

apns2 requires Node.js v8.10 or later