To use this library, node must be v6 or higher.
npm install apns-http2
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
});
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);
});