Uses the resolved promise from the adapter

This commit is contained in:
Florent Vilmart
2016-03-12 15:20:52 -05:00
parent 4d401d9daa
commit a392c088d8
5 changed files with 57 additions and 23 deletions

View File

@@ -143,8 +143,8 @@ describe('PushController', () => {
} }
}) })
return Promise.resolve({ return Promise.resolve({
body: body, error: null
installations: installations payload: body,
}) })
}, },
getValidPushTypes: function() { getValidPushTypes: function() {
@@ -195,8 +195,8 @@ describe('PushController', () => {
expect(1).toEqual(installation.badge); expect(1).toEqual(installation.badge);
}) })
return Promise.resolve({ return Promise.resolve({
body: body, payload: body,
installations: installations error: null
}) })
}, },
getValidPushTypes: function() { getValidPushTypes: function() {
@@ -233,9 +233,10 @@ describe('PushController', () => {
send: function(body, installations) { send: function(body, installations) {
var badge = body.data.badge; var badge = body.data.badge;
return Promise.resolve({ return Promise.resolve({
body: body, error: null,
installations: installations response: "OK!",
}) payload: body
});
}, },
getValidPushTypes: function() { getValidPushTypes: function() {
return ["ios"]; return ["ios"];
@@ -271,7 +272,9 @@ describe('PushController', () => {
var pushAdapter = { var pushAdapter = {
send: function(body, installations) { send: function(body, installations) {
return Promise.resolve(); return Promise.resolve({
error:null
});
}, },
getValidPushTypes: function() { getValidPushTypes: function() {
return ["ios"]; return ["ios"];

View File

@@ -66,6 +66,12 @@ function APNS(args) {
}); });
conn.on('transmitted', function(notification, device) { conn.on('transmitted', function(notification, device) {
if (device.callback) {
device.callback(null, {
notification: notification,
device: device
});
}
console.log('APNS Connection %d Notification transmitted to %s', conn.index, device.token.toString('hex')); console.log('APNS Connection %d Notification transmitted to %s', conn.index, device.token.toString('hex'));
}); });
@@ -91,11 +97,14 @@ APNS.prototype.send = function(data, devices) {
let coreData = data.data; let coreData = data.data;
let expirationTime = data['expiration_time']; let expirationTime = data['expiration_time'];
let notification = generateNotification(coreData, expirationTime); let notification = generateNotification(coreData, expirationTime);
for (let device of devices) {
let promises = devices.map((device) => {
let qualifiedConnIndexs = chooseConns(this.conns, device); let qualifiedConnIndexs = chooseConns(this.conns, device);
// We can not find a valid conn, just ignore this device // We can not find a valid conn, just ignore this device
if (qualifiedConnIndexs.length == 0) { if (qualifiedConnIndexs.length == 0) {
continue; return Promise.resolve({
err: 'No connection available'
});
} }
let conn = this.conns[qualifiedConnIndexs[0]]; let conn = this.conns[qualifiedConnIndexs[0]];
let apnDevice = new apn.Device(device.deviceToken); let apnDevice = new apn.Device(device.deviceToken);
@@ -104,9 +113,19 @@ APNS.prototype.send = function(data, devices) {
if (device.appIdentifier) { if (device.appIdentifier) {
apnDevice.appIdentifier = device.appIdentifier; apnDevice.appIdentifier = device.appIdentifier;
} }
conn.pushNotification(notification, apnDevice); return new Promise((resolve, reject) => {
apnDevice.callback = (err, res) => {
resolve({
error: err,
response: res,
payload: notification,
deviceType: 'ios'
});
} }
return Parse.Promise.as(); conn.pushNotification(notification, apnDevice);
});
});
return Parse.Promise.when(promises);
} }
function handleTransmissionError(conns, errCode, notification, apnDevice) { function handleTransmissionError(conns, errCode, notification, apnDevice) {
@@ -133,7 +152,12 @@ function handleTransmissionError(conns, errCode, notification, apnDevice) {
} }
// There is no more available conns, we give up in this case // There is no more available conns, we give up in this case
if (newConnIndex < 0 || newConnIndex >= conns.length) { if (newConnIndex < 0 || newConnIndex >= conns.length) {
console.log('APNS can not find vaild connection for %j', apnDevice.token); if (apnDevice.callback) {
apnDevice.callback({
error: `APNS can not find vaild connection for ${apnDevice.token}`,
code: errCode
});
}
return; return;
} }

View File

@@ -92,6 +92,7 @@ export class PushController extends AdaptableController {
}).then(() => { }).then(() => {
return rest.find(config, auth, '_Installation', where); return rest.find(config, auth, '_Installation', where);
}).then((response) => { }).then((response) => {
this.updatePushStatus({status: "running"}, {status:"pending", objectId: pushStatus.objectId}, config);
if (body.data && body.data.badge && body.data.badge == "Increment") { if (body.data && body.data.badge && body.data.badge == "Increment") {
// Collect the badges to reduce the # of calls // Collect the badges to reduce the # of calls
let badgeInstallationsMap = response.results.reduce((map, installation) => { let badgeInstallationsMap = response.results.reduce((map, installation) => {
@@ -117,8 +118,9 @@ export class PushController extends AdaptableController {
return Promise.all(promises); return Promise.all(promises);
} }
return pushAdapter.send(body, response.results, pushStatus); return pushAdapter.send(body, response.results, pushStatus);
}).then(() => { }).then((results) => {
return this.updatePushStatus({status: "running"}, pushStatus, config); console.log(results);
return Promise.resolve(results);
}); });
} }
@@ -139,8 +141,8 @@ export class PushController extends AdaptableController {
return restWrite.execute(); return restWrite.execute();
} }
updatePushStatus(update, pushStatus, config) { updatePushStatus(update, where, config) {
let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', {objectId: pushStatus.objectId, "status": "pending"}, update); let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', where, update);
return restWrite.execute(); return restWrite.execute();
} }

View File

@@ -21,7 +21,7 @@ function GCM(args) {
* @param {Array} devices A array of devices * @param {Array} devices A array of devices
* @returns {Object} A promise which is resolved after we get results from gcm * @returns {Object} A promise which is resolved after we get results from gcm
*/ */
GCM.prototype.send = function(data, devices) { GCM.prototype.send = function(data, devices, callback) {
let pushId = cryptoUtils.newObjectId(); let pushId = cryptoUtils.newObjectId();
let timeStamp = Date.now(); let timeStamp = Date.now();
let expirationTime; let expirationTime;
@@ -52,7 +52,12 @@ GCM.prototype.send = function(data, devices) {
request: message, request: message,
response: response response: response
}); });
sendPromise.resolve(); sendPromise.resolve({
error: error,
response: response,
payload: message,
deviceType: 'android'
});
}); });
sendPromises.push(sendPromise); sendPromises.push(sendPromise);
} }