const apn = require('apn');
let options = {};
let apnId = "85c5665b9f62c38262aee87f853e3f66fca2ba8553a233649077975f4aad60cd";
let apnId2 = "3eb248303b041ab16768bb02e4c27a1475b51b703d929daaf4bb38d343e1ae2c";
options.cert = "/Users/sapinder/Projects/Certificates/PushChatCert.pem";
options.key = "/Users/sapinder/Projects/Certificates/PushChatKey.pem";
options.pphrase = "apple";

let apnConnection = new apn.Connection(options);
const logger = require('../../../logger');
let myDevice = new apn.Device(apnId);
let myDevice2 = new apn.Device(apnId2);
let note = new apn.Notification();
//note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
//note.payload = {'COMMAND': "Hello this is a test push."}; 
note.setAlertText("Hello, from node-apn! This is direct push.");
note.badge = 1;
note.category="ACTIONABLE";
note.payload = {data: {"name":"sapinder"}}; 

apnConnection.pushNotification(note, [myDevice,myDevice2]);


apnConnection.on("transmitted", function(notification, device) {
    logger.info(`notification => ${notification}`)
    logger.info(`Notification transmitted to => ${device.token.toString("hex")}`); 
     
});

apnConnection.on("transmissionError", function(errCode, notification, device) {
    logger.error(`Notification caused error=>  ${errCode}`);
    logger.info(`for device=>  ${device} ${notification}`);
    if (errCode === 8) {
        logger.info("A error code of 8 indicates that the device token is invalid. This could be for a number of reasons - are you using the correct environment? i.e. Production vs. Sandbox");      
    }
});
