reverts to use binary APNs
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
],
|
],
|
||||||
"license": "BSD-3-Clause",
|
"license": "BSD-3-Clause",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"apn": "^1.7.5",
|
||||||
"aws-sdk": "~2.2.33",
|
"aws-sdk": "~2.2.33",
|
||||||
"babel-polyfill": "^6.5.0",
|
"babel-polyfill": "^6.5.0",
|
||||||
"babel-runtime": "^6.5.0",
|
"babel-runtime": "^6.5.0",
|
||||||
@@ -28,7 +29,6 @@
|
|||||||
"deepcopy": "^0.6.1",
|
"deepcopy": "^0.6.1",
|
||||||
"express": "^4.13.4",
|
"express": "^4.13.4",
|
||||||
"gcloud": "^0.28.0",
|
"gcloud": "^0.28.0",
|
||||||
"http2": "flovilmart/node-http2",
|
|
||||||
"mailgun-js": "^0.7.7",
|
"mailgun-js": "^0.7.7",
|
||||||
"mime": "^1.3.4",
|
"mime": "^1.3.4",
|
||||||
"mongodb": "~2.1.0",
|
"mongodb": "~2.1.0",
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
'use strict';
|
|
||||||
var APNS = require('../src/APNS');
|
var APNS = require('../src/APNS');
|
||||||
|
|
||||||
describe('APNS', () => {
|
describe('APNS', () => {
|
||||||
@@ -10,13 +9,17 @@ describe('APNS', () => {
|
|||||||
production: true,
|
production: true,
|
||||||
bundleId: 'bundleId'
|
bundleId: 'bundleId'
|
||||||
}
|
}
|
||||||
var apns = APNS(args);
|
var apns = new APNS(args);
|
||||||
|
|
||||||
var apnsConfiguration = apns.getConfiguration();
|
expect(apns.conns.length).toBe(1);
|
||||||
expect(apnsConfiguration.bundleId).toBe(args.bundleId);
|
var apnsConnection = apns.conns[0];
|
||||||
expect(apnsConfiguration.cert).toBe(args.cert);
|
expect(apnsConnection.index).toBe(0);
|
||||||
expect(apnsConfiguration.key).toBe(args.key);
|
expect(apnsConnection.bundleId).toBe(args.bundleId);
|
||||||
expect(apnsConfiguration.production).toBe(args.production);
|
// TODO: Remove this checking onec we inject APNS
|
||||||
|
var prodApnsOptions = apnsConnection.options;
|
||||||
|
expect(prodApnsOptions.cert).toBe(args.cert);
|
||||||
|
expect(prodApnsOptions.key).toBe(args.key);
|
||||||
|
expect(prodApnsOptions.production).toBe(args.production);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -36,18 +39,24 @@ describe('APNS', () => {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
var apns = APNS(args);
|
var apns = new APNS(args);
|
||||||
var devApnsConfiguration = apns.getConfiguration('bundleId');
|
expect(apns.conns.length).toBe(2);
|
||||||
expect(devApnsConfiguration.cert).toBe(args[0].cert);
|
var devApnsConnection = apns.conns[1];
|
||||||
expect(devApnsConfiguration.key).toBe(args[0].key);
|
expect(devApnsConnection.index).toBe(1);
|
||||||
expect(devApnsConfiguration.production).toBe(args[0].production);
|
var devApnsOptions = devApnsConnection.options;
|
||||||
expect(devApnsConfiguration.bundleId).toBe(args[0].bundleId);
|
expect(devApnsOptions.cert).toBe(args[0].cert);
|
||||||
|
expect(devApnsOptions.key).toBe(args[0].key);
|
||||||
|
expect(devApnsOptions.production).toBe(args[0].production);
|
||||||
|
expect(devApnsConnection.bundleId).toBe(args[0].bundleId);
|
||||||
|
|
||||||
var prodApnsConfiguration = apns.getConfiguration('bundleIdAgain');
|
var prodApnsConnection = apns.conns[0];
|
||||||
expect(prodApnsConfiguration.cert).toBe(args[1].cert);
|
expect(prodApnsConnection.index).toBe(0);
|
||||||
expect(prodApnsConfiguration.key).toBe(args[1].key);
|
// TODO: Remove this checking onec we inject APNS
|
||||||
expect(prodApnsConfiguration.production).toBe(args[1].production);
|
var prodApnsOptions = prodApnsConnection.options;
|
||||||
expect(prodApnsConfiguration.bundleId).toBe(args[1].bundleId);
|
expect(prodApnsOptions.cert).toBe(args[1].cert);
|
||||||
|
expect(prodApnsOptions.key).toBe(args[1].key);
|
||||||
|
expect(prodApnsOptions.production).toBe(args[1].production);
|
||||||
|
expect(prodApnsOptions.bundleId).toBe(args[1].bundleId);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -64,14 +73,56 @@ describe('APNS', () => {
|
|||||||
};
|
};
|
||||||
var expirationTime = 1454571491354
|
var expirationTime = 1454571491354
|
||||||
|
|
||||||
var notification = APNS.generateNotification(data);
|
var notification = APNS.generateNotification(data, expirationTime);
|
||||||
expect(notification.aps.alert).toEqual(data.alert);
|
|
||||||
expect(notification.aps.badge).toEqual(data.badge);
|
expect(notification.alert).toEqual(data.alert);
|
||||||
expect(notification.aps.sound).toEqual(data.sound);
|
expect(notification.badge).toEqual(data.badge);
|
||||||
expect(notification.aps['content-available']).toEqual(1);
|
expect(notification.sound).toEqual(data.sound);
|
||||||
expect(notification.aps.category).toEqual(data.category);
|
expect(notification.contentAvailable).toEqual(1);
|
||||||
expect(notification.key).toEqual('value');
|
expect(notification.category).toEqual(data.category);
|
||||||
expect(notification.keyAgain).toEqual('valueAgain');
|
expect(notification.payload).toEqual({
|
||||||
|
'key': 'value',
|
||||||
|
'keyAgain': 'valueAgain'
|
||||||
|
});
|
||||||
|
expect(notification.expiry).toEqual(expirationTime);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can choose conns for device without appIdentifier', (done) => {
|
||||||
|
// Mock conns
|
||||||
|
var conns = [
|
||||||
|
{
|
||||||
|
bundleId: 'bundleId'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
bundleId: 'bundleIdAgain'
|
||||||
|
}
|
||||||
|
];
|
||||||
|
// Mock device
|
||||||
|
var device = {};
|
||||||
|
|
||||||
|
var qualifiedConns = APNS.chooseConns(conns, device);
|
||||||
|
expect(qualifiedConns).toEqual([0, 1]);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can choose conns for device with valid appIdentifier', (done) => {
|
||||||
|
// Mock conns
|
||||||
|
var conns = [
|
||||||
|
{
|
||||||
|
bundleId: 'bundleId'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
bundleId: 'bundleIdAgain'
|
||||||
|
}
|
||||||
|
];
|
||||||
|
// Mock device
|
||||||
|
var device = {
|
||||||
|
appIdentifier: 'bundleId'
|
||||||
|
};
|
||||||
|
|
||||||
|
var qualifiedConns = APNS.chooseConns(conns, device);
|
||||||
|
expect(qualifiedConns).toEqual([0]);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -79,7 +130,7 @@ describe('APNS', () => {
|
|||||||
// Mock conns
|
// Mock conns
|
||||||
var conns = [
|
var conns = [
|
||||||
{
|
{
|
||||||
bundleId: 'bundleId',
|
bundleId: 'bundleId'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
bundleId: 'bundleIdAgain'
|
bundleId: 'bundleIdAgain'
|
||||||
@@ -89,18 +140,143 @@ describe('APNS', () => {
|
|||||||
var device = {
|
var device = {
|
||||||
appIdentifier: 'invalid'
|
appIdentifier: 'invalid'
|
||||||
};
|
};
|
||||||
let apns = APNS(conns);
|
|
||||||
var config = apns.getConfiguration(device.appIdentifier);
|
var qualifiedConns = APNS.chooseConns(conns, device);
|
||||||
expect(config).toBeUndefined();
|
expect(qualifiedConns).toEqual([]);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can handle transmission error when notification is not in cache or device is missing', (done) => {
|
||||||
|
// Mock conns
|
||||||
|
var conns = [];
|
||||||
|
var errorCode = 1;
|
||||||
|
var notification = undefined;
|
||||||
|
var device = {};
|
||||||
|
|
||||||
|
APNS.handleTransmissionError(conns, errorCode, notification, device);
|
||||||
|
|
||||||
|
var notification = {};
|
||||||
|
var device = undefined;
|
||||||
|
|
||||||
|
APNS.handleTransmissionError(conns, errorCode, notification, device);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can handle transmission error when there are other qualified conns', (done) => {
|
||||||
|
// Mock conns
|
||||||
|
var conns = [
|
||||||
|
{
|
||||||
|
pushNotification: jasmine.createSpy('pushNotification'),
|
||||||
|
bundleId: 'bundleId1'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pushNotification: jasmine.createSpy('pushNotification'),
|
||||||
|
bundleId: 'bundleId1'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pushNotification: jasmine.createSpy('pushNotification'),
|
||||||
|
bundleId: 'bundleId2'
|
||||||
|
},
|
||||||
|
];
|
||||||
|
var errorCode = 1;
|
||||||
|
var notification = {};
|
||||||
|
var apnDevice = {
|
||||||
|
connIndex: 0,
|
||||||
|
appIdentifier: 'bundleId1'
|
||||||
|
};
|
||||||
|
|
||||||
|
APNS.handleTransmissionError(conns, errorCode, notification, apnDevice);
|
||||||
|
|
||||||
|
expect(conns[0].pushNotification).not.toHaveBeenCalled();
|
||||||
|
expect(conns[1].pushNotification).toHaveBeenCalled();
|
||||||
|
expect(conns[2].pushNotification).not.toHaveBeenCalled();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can handle transmission error when there is no other qualified conns', (done) => {
|
||||||
|
// Mock conns
|
||||||
|
var conns = [
|
||||||
|
{
|
||||||
|
pushNotification: jasmine.createSpy('pushNotification'),
|
||||||
|
bundleId: 'bundleId1'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pushNotification: jasmine.createSpy('pushNotification'),
|
||||||
|
bundleId: 'bundleId1'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pushNotification: jasmine.createSpy('pushNotification'),
|
||||||
|
bundleId: 'bundleId1'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pushNotification: jasmine.createSpy('pushNotification'),
|
||||||
|
bundleId: 'bundleId2'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pushNotification: jasmine.createSpy('pushNotification'),
|
||||||
|
bundleId: 'bundleId1'
|
||||||
|
}
|
||||||
|
];
|
||||||
|
var errorCode = 1;
|
||||||
|
var notification = {};
|
||||||
|
var apnDevice = {
|
||||||
|
connIndex: 2,
|
||||||
|
appIdentifier: 'bundleId1'
|
||||||
|
};
|
||||||
|
|
||||||
|
APNS.handleTransmissionError(conns, errorCode, notification, apnDevice);
|
||||||
|
|
||||||
|
expect(conns[0].pushNotification).not.toHaveBeenCalled();
|
||||||
|
expect(conns[1].pushNotification).not.toHaveBeenCalled();
|
||||||
|
expect(conns[2].pushNotification).not.toHaveBeenCalled();
|
||||||
|
expect(conns[3].pushNotification).not.toHaveBeenCalled();
|
||||||
|
expect(conns[4].pushNotification).toHaveBeenCalled();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can handle transmission error when device has no appIdentifier', (done) => {
|
||||||
|
// Mock conns
|
||||||
|
var conns = [
|
||||||
|
{
|
||||||
|
pushNotification: jasmine.createSpy('pushNotification'),
|
||||||
|
bundleId: 'bundleId1'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pushNotification: jasmine.createSpy('pushNotification'),
|
||||||
|
bundleId: 'bundleId2'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pushNotification: jasmine.createSpy('pushNotification'),
|
||||||
|
bundleId: 'bundleId3'
|
||||||
|
},
|
||||||
|
];
|
||||||
|
var errorCode = 1;
|
||||||
|
var notification = {};
|
||||||
|
var apnDevice = {
|
||||||
|
connIndex: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
APNS.handleTransmissionError(conns, errorCode, notification, apnDevice);
|
||||||
|
|
||||||
|
expect(conns[0].pushNotification).not.toHaveBeenCalled();
|
||||||
|
expect(conns[1].pushNotification).not.toHaveBeenCalled();
|
||||||
|
expect(conns[2].pushNotification).toHaveBeenCalled();
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can send APNS notification', (done) => {
|
it('can send APNS notification', (done) => {
|
||||||
var args = {
|
var args = {
|
||||||
|
cert: 'prodCert.pem',
|
||||||
|
key: 'prodKey.pem',
|
||||||
production: true,
|
production: true,
|
||||||
bundleId: 'bundleId'
|
bundleId: 'bundleId'
|
||||||
}
|
}
|
||||||
var apns = APNS(args);
|
var apns = new APNS(args);
|
||||||
|
var conn = {
|
||||||
|
pushNotification: jasmine.createSpy('send'),
|
||||||
|
bundleId: 'bundleId'
|
||||||
|
};
|
||||||
|
apns.conns = [ conn ];
|
||||||
// Mock data
|
// Mock data
|
||||||
var expirationTime = 1454571491354
|
var expirationTime = 1454571491354
|
||||||
var data = {
|
var data = {
|
||||||
@@ -117,18 +293,15 @@ describe('APNS', () => {
|
|||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
apns.send(data, devices).then((results) => {
|
var promise = apns.send(data, devices);
|
||||||
let isArray = Array.isArray(results);
|
expect(conn.pushNotification).toHaveBeenCalled();
|
||||||
expect(isArray).toBe(true);
|
var args = conn.pushNotification.calls.first().args;
|
||||||
expect(results.length).toBe(1);
|
var notification = args[0];
|
||||||
// No provided certificates
|
expect(notification.alert).toEqual(data.data.alert);
|
||||||
expect(results[0].status).toBe(403);
|
expect(notification.expiry).toEqual(data['expiration_time']);
|
||||||
expect(results[0].device).toEqual(devices[0]);
|
var apnDevice = args[1]
|
||||||
expect(results[0].transmitted).toBe(false);
|
expect(apnDevice.connIndex).toEqual(0);
|
||||||
done();
|
expect(apnDevice.appIdentifier).toEqual('bundleId');
|
||||||
}, (err) => {
|
done();
|
||||||
fail('should not fail');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -29,9 +29,7 @@ describe('ParsePushAdapter', () => {
|
|||||||
var parsePushAdapter = new ParsePushAdapter(pushConfig);
|
var parsePushAdapter = new ParsePushAdapter(pushConfig);
|
||||||
// Check ios
|
// Check ios
|
||||||
var iosSender = parsePushAdapter.senderMap['ios'];
|
var iosSender = parsePushAdapter.senderMap['ios'];
|
||||||
expect(iosSender).not.toBe(undefined);
|
expect(iosSender instanceof APNS).toBe(true);
|
||||||
expect(typeof iosSender.send).toEqual('function');
|
|
||||||
expect(typeof iosSender.getConfiguration).toEqual('function');
|
|
||||||
// Check android
|
// Check android
|
||||||
var androidSender = parsePushAdapter.senderMap['android'];
|
var androidSender = parsePushAdapter.senderMap['android'];
|
||||||
expect(androidSender instanceof GCM).toBe(true);
|
expect(androidSender instanceof GCM).toBe(true);
|
||||||
|
|||||||
292
src/APNS.js
292
src/APNS.js
@@ -1,30 +1,9 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const Parse = require('parse/node').Parse;
|
const Parse = require('parse/node').Parse;
|
||||||
const http = require('http2');
|
// TODO: apn does not support the new HTTP/2 protocal. It is fine to use it in V1,
|
||||||
const fs = require('fs');
|
// but probably we will replace it in the future.
|
||||||
const path = require('path');
|
const apn = require('apn');
|
||||||
const urlParse = require('url').parse;
|
|
||||||
|
|
||||||
const DEV_PUSH_SERVER = 'api.development.push.apple.com';
|
|
||||||
const PROD_PUSH_SERVER = 'api.push.apple.com';
|
|
||||||
|
|
||||||
const createRequestOptions = (opts, device, body) => {
|
|
||||||
let domain = opts.production === true ? PROD_PUSH_SERVER : DEV_PUSH_SERVER;
|
|
||||||
var options = urlParse(`https://${domain}/3/device/${device.deviceToken}`);
|
|
||||||
options.method = 'POST';
|
|
||||||
options.headers = {
|
|
||||||
'apns-expiration': opts.expiration || 0,
|
|
||||||
'apns-priority': opts.priority || 10,
|
|
||||||
'apns-topic': opts.bundleId || opts['apns-topic'],
|
|
||||||
'content-length': body.length
|
|
||||||
};
|
|
||||||
options.key = opts.key;
|
|
||||||
options.cert = opts.cert;
|
|
||||||
options.pfx = opts.pfx;
|
|
||||||
options.passphrase = opts.passphrase;
|
|
||||||
return Object.assign({}, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new connection to the APN service.
|
* Create a new connection to the APN service.
|
||||||
@@ -37,115 +16,170 @@ const createRequestOptions = (opts, device, body) => {
|
|||||||
* @param {String} args.bundleId The bundleId for cert
|
* @param {String} args.bundleId The bundleId for cert
|
||||||
* @param {Boolean} args.production Specifies which environment to connect to: Production (if true) or Sandbox
|
* @param {Boolean} args.production Specifies which environment to connect to: Production (if true) or Sandbox
|
||||||
*/
|
*/
|
||||||
function APNS(options) {
|
function APNS(args) {
|
||||||
|
// Since for ios, there maybe multiple cert/key pairs,
|
||||||
if (!Array.isArray(options)) {
|
// typePushConfig can be an array.
|
||||||
options = [options];
|
let apnsArgsList = [];
|
||||||
|
if (Array.isArray(args)) {
|
||||||
|
apnsArgsList = apnsArgsList.concat(args);
|
||||||
|
} else if (typeof args === 'object') {
|
||||||
|
apnsArgsList.push(args);
|
||||||
|
} else {
|
||||||
|
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
||||||
|
'APNS Configuration is invalid');
|
||||||
}
|
}
|
||||||
|
|
||||||
let agents = {};
|
this.conns = [];
|
||||||
|
for (let apnsArgs of apnsArgsList) {
|
||||||
let optionsByBundle = options.reduce((memo, option) => {
|
let conn = new apn.Connection(apnsArgs);
|
||||||
try {
|
if (!apnsArgs.bundleId) {
|
||||||
if (option.key && option.cert) {
|
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
||||||
option.key = fs.readFileSync(option.key);
|
'BundleId is mssing for %j', apnsArgs);
|
||||||
option.cert = fs.readFileSync(option.cert);
|
|
||||||
} else if (option.pfx) {
|
|
||||||
option.pfx = fs.readFileSync(option.pfx);
|
|
||||||
} else {
|
|
||||||
throw 'Either cert AND key, OR pfx is required'
|
|
||||||
}
|
|
||||||
} catch(e) {
|
|
||||||
if (!process.env.NODE_ENV == 'test' || options.enforceCertificates) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
option.agent = new http.Agent({
|
conn.bundleId = apnsArgs.bundleId;
|
||||||
key: option.key,
|
// Set the priority of the conns, prod cert has higher priority
|
||||||
cert: option.cert,
|
if (apnsArgs.production) {
|
||||||
pfx: option.pfx,
|
conn.priority = 0;
|
||||||
passphrase: option.passphrase
|
} else {
|
||||||
|
conn.priority = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set apns client callbacks
|
||||||
|
conn.on('connected', () => {
|
||||||
|
console.log('APNS Connection %d Connected', conn.index);
|
||||||
});
|
});
|
||||||
memo[option.bundleId] = option;
|
|
||||||
return memo;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
let getConfiguration = (bundleIdentifier) => {
|
conn.on('transmissionError', (errCode, notification, apnDevice) => {
|
||||||
let configuration;
|
handleTransmissionError(this.conns, errCode, notification, apnDevice);
|
||||||
if (bundleIdentifier) {
|
});
|
||||||
configuration = optionsByBundle[bundleIdentifier];
|
|
||||||
if (!configuration) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!configuration) {
|
|
||||||
configuration = options[0];
|
|
||||||
}
|
|
||||||
return configuration;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
conn.on('timeout', () => {
|
||||||
* Send apns request.
|
console.log('APNS Connection %d Timeout', conn.index);
|
||||||
* @param {Object} data The data we need to send, the format is the same with api request body
|
});
|
||||||
* @param {Array} devices A array of device tokens
|
|
||||||
* @returns {Object} A promises that resolves with each notificaiton sending promise
|
|
||||||
*/
|
|
||||||
let send = function(data, devices) {
|
|
||||||
// Make sure devices are in an array
|
|
||||||
if (!Array.isArray(devices)) {
|
|
||||||
devices = [devices];
|
|
||||||
}
|
|
||||||
|
|
||||||
let coreData = data.data;
|
conn.on('disconnected', () => {
|
||||||
let expirationTime = data['expiration_time'];
|
console.log('APNS Connection %d Disconnected', conn.index);
|
||||||
let notification = generateNotification(coreData);
|
});
|
||||||
let notificationString = JSON.stringify(notification);
|
|
||||||
let buffer = new Buffer(notificationString);
|
|
||||||
|
|
||||||
let promises = devices.map((device) => {
|
conn.on('socketError', () => {
|
||||||
return new Promise((resolve, reject) => {
|
console.log('APNS Connection %d Socket Error', conn.index);
|
||||||
let configuration = getConfiguration(device.appIdentifier);
|
});
|
||||||
if (!configuration) {
|
|
||||||
return Promise.reject({
|
conn.on('transmitted', function(notification, device) {
|
||||||
status: -1,
|
if (device.callback) {
|
||||||
device: device,
|
device.callback({
|
||||||
response: {"error": "No configuration set for that appIdentifier"},
|
notification: notification,
|
||||||
transmitted: false
|
transmitted: true,
|
||||||
})
|
device: device
|
||||||
}
|
|
||||||
configuration = Object.assign({}, configuration, {expiration: expirationTime })
|
|
||||||
let requestOptions = createRequestOptions(configuration, device, buffer);
|
|
||||||
let req = configuration.agent.request(requestOptions, (response) => {
|
|
||||||
response.setEncoding('utf8');
|
|
||||||
var chunks = "";
|
|
||||||
response.on('data', (chunk) => {
|
|
||||||
chunks+=chunk;
|
|
||||||
});
|
|
||||||
response.on('end', () => {
|
|
||||||
let body;
|
|
||||||
try{
|
|
||||||
body = JSON.parse(chunks);
|
|
||||||
} catch (e) {
|
|
||||||
body = {};
|
|
||||||
}
|
|
||||||
resolve({ status: response.statusCode,
|
|
||||||
response: body,
|
|
||||||
headers: response.headers,
|
|
||||||
device: device,
|
|
||||||
transmitted: response.statusCode == 200 });
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
req.write(buffer);
|
}
|
||||||
req.end();
|
console.log('APNS Connection %d Notification transmitted to %s', conn.index, device.token.toString('hex'));
|
||||||
});
|
|
||||||
});
|
});
|
||||||
return Promise.all(promises);
|
|
||||||
|
this.conns.push(conn);
|
||||||
|
}
|
||||||
|
// Sort the conn based on priority ascending, high pri first
|
||||||
|
this.conns.sort((s1, s2) => {
|
||||||
|
return s1.priority - s2.priority;
|
||||||
|
});
|
||||||
|
// Set index of conns
|
||||||
|
for (let index = 0; index < this.conns.length; index++) {
|
||||||
|
this.conns[index].index = index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send apns request.
|
||||||
|
* @param {Object} data The data we need to send, the format is the same with api request body
|
||||||
|
* @param {Array} devices A array of devices
|
||||||
|
* @returns {Object} A promise which is resolved immediately
|
||||||
|
*/
|
||||||
|
APNS.prototype.send = function(data, devices) {
|
||||||
|
let coreData = data.data;
|
||||||
|
let expirationTime = data['expiration_time'];
|
||||||
|
let notification = generateNotification(coreData, expirationTime);
|
||||||
|
|
||||||
|
let promises = devices.map((device) => {
|
||||||
|
let qualifiedConnIndexs = chooseConns(this.conns, device);
|
||||||
|
// We can not find a valid conn, just ignore this device
|
||||||
|
if (qualifiedConnIndexs.length == 0) {
|
||||||
|
return Promise.resolve({
|
||||||
|
transmitted: false,
|
||||||
|
result: {error: 'No connection available'}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
let conn = this.conns[qualifiedConnIndexs[0]];
|
||||||
|
let apnDevice = new apn.Device(device.deviceToken);
|
||||||
|
apnDevice.connIndex = qualifiedConnIndexs[0];
|
||||||
|
// Add additional appIdentifier info to apn device instance
|
||||||
|
if (device.appIdentifier) {
|
||||||
|
apnDevice.appIdentifier = device.appIdentifier;
|
||||||
|
}
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
apnDevice.callback = resolve;
|
||||||
|
conn.pushNotification(notification, apnDevice);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return Parse.Promise.when(promises);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleTransmissionError(conns, errCode, notification, apnDevice) {
|
||||||
|
// This means the error notification is not in the cache anymore or the recepient is missing,
|
||||||
|
// we just ignore this case
|
||||||
|
if (!notification || !apnDevice) {
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.freeze({
|
// If currentConn can not send the push notification, we try to use the next available conn.
|
||||||
send: send,
|
// Since conns is sorted by priority, the next conn means the next low pri conn.
|
||||||
getConfiguration: getConfiguration
|
// If there is no conn available, we give up on sending the notification to that device.
|
||||||
})
|
let qualifiedConnIndexs = chooseConns(conns, apnDevice);
|
||||||
|
let currentConnIndex = apnDevice.connIndex;
|
||||||
|
|
||||||
|
let newConnIndex = -1;
|
||||||
|
// Find the next element of currentConnIndex in qualifiedConnIndexs
|
||||||
|
for (let index = 0; index < qualifiedConnIndexs.length - 1; index++) {
|
||||||
|
if (qualifiedConnIndexs[index] === currentConnIndex) {
|
||||||
|
newConnIndex = qualifiedConnIndexs[index + 1];
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// There is no more available conns, we give up in this case
|
||||||
|
if (newConnIndex < 0 || newConnIndex >= conns.length) {
|
||||||
|
if (apnDevice.callback) {
|
||||||
|
apnDevice.callback({
|
||||||
|
response: {error: `APNS can not find vaild connection for ${apnDevice.token}`, code: errCode},
|
||||||
|
status: errCode,
|
||||||
|
transmitted: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let newConn = conns[newConnIndex];
|
||||||
|
// Update device conn info
|
||||||
|
apnDevice.connIndex = newConnIndex;
|
||||||
|
// Use the new conn to send the notification
|
||||||
|
newConn.pushNotification(notification, apnDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
function chooseConns(conns, device) {
|
||||||
|
// If device does not have appIdentifier, all conns maybe proper connections.
|
||||||
|
// Otherwise we try to match the appIdentifier with bundleId
|
||||||
|
let qualifiedConns = [];
|
||||||
|
for (let index = 0; index < conns.length; index++) {
|
||||||
|
let conn = conns[index];
|
||||||
|
// If the device we need to send to does not have
|
||||||
|
// appIdentifier, any conn could be a qualified connection
|
||||||
|
if (!device.appIdentifier || device.appIdentifier === '') {
|
||||||
|
qualifiedConns.push(index);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (device.appIdentifier === conn.bundleId) {
|
||||||
|
qualifiedConns.push(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return qualifiedConns;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -154,12 +188,12 @@ function APNS(options) {
|
|||||||
* @returns {Object} A apns notification
|
* @returns {Object} A apns notification
|
||||||
*/
|
*/
|
||||||
function generateNotification(coreData, expirationTime) {
|
function generateNotification(coreData, expirationTime) {
|
||||||
|
let notification = new apn.notification();
|
||||||
let payload = {};
|
let payload = {};
|
||||||
let notification = {};
|
|
||||||
for (let key in coreData) {
|
for (let key in coreData) {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case 'alert':
|
case 'alert':
|
||||||
notification.alert = coreData.alert;
|
notification.setAlertText(coreData.alert);
|
||||||
break;
|
break;
|
||||||
case 'badge':
|
case 'badge':
|
||||||
notification.badge = coreData.badge;
|
notification.badge = coreData.badge;
|
||||||
@@ -168,10 +202,9 @@ function generateNotification(coreData, expirationTime) {
|
|||||||
notification.sound = coreData.sound;
|
notification.sound = coreData.sound;
|
||||||
break;
|
break;
|
||||||
case 'content-available':
|
case 'content-available':
|
||||||
|
notification.setNewsstandAvailable(true);
|
||||||
let isAvailable = coreData['content-available'] === 1;
|
let isAvailable = coreData['content-available'] === 1;
|
||||||
if (isAvailable) {
|
notification.setContentAvailable(isAvailable);
|
||||||
notification['content-available'] = 1;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'category':
|
case 'category':
|
||||||
notification.category = coreData.category;
|
notification.category = coreData.category;
|
||||||
@@ -181,11 +214,14 @@ function generateNotification(coreData, expirationTime) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
payload.aps = notification;
|
notification.payload = payload;
|
||||||
return payload;
|
notification.expiry = expirationTime;
|
||||||
|
return notification;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
|
if (typeof process !== 'undefined' && process.env.NODE_ENV === 'test') {
|
||||||
APNS.generateNotification = generateNotification;
|
APNS.generateNotification = generateNotification;
|
||||||
|
APNS.chooseConns = chooseConns;
|
||||||
|
APNS.handleTransmissionError = handleTransmissionError;
|
||||||
}
|
}
|
||||||
module.exports = APNS;
|
module.exports = APNS;
|
||||||
|
|||||||
Reference in New Issue
Block a user