harry1064 / APNS2

MIT License
1 stars 6 forks source link

APNS2

npm version

To use this library, node must be v6 or higher.

Installation

npm install apns-http2

Create Client

Client using .p12 and passphrase

const APNS = require('apns-http2');
const fs = require('fs');
const apnsClient = new APNS({
    pfx: fs.readFileSync('/path/to/Certificates.p12'),
    passphrase: 'test',
    apns_topic: 'com.yourapp.com',
    production: false
});

Client using cert and key in .pem extension

const APNS = require('apns-http2');
const fs = require('fs');
const apnsClient = new APNS({
    cert: fs.readFileSync('/path/to/Certificate.pem'),
    key: fs.readFileSync('/path/to/key.pem'),
    apns_topic: 'com.yourapp.com',
    production: false
});

Sending Push Notification

const Notification = APNS.Notification;

const aps = {
        badge : 9,
        sound : "bingbong.aiff"
    };
const alert = {
            title : "Game Request",
            body : "Bob wants to play poker"
        };
const deviceToken = '00fc13adff785122b4ad28809a3420982341241421348097878e577c991de8f0';
const options = {
    'aps' : aps,
    'alert' : alert,
    'priority' : 10,
    'expiration' : 0
}
const notification = new Notification(deviceToken, options);
apnsClient.send(notification)
.then((res) => {
    console.log("response:  ");
    console.dir(res);
}).catch((err) => {
    console.log('APNS ERROR: ' + err);
});

Payload Example can be found here Notification JSON payload examples

For generating certificate Apple guide to generate push certificate