Uses the resolved promise from the adapter
This commit is contained in:
@@ -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"];
|
||||||
|
|||||||
36
src/APNS.js
36
src/APNS.js
@@ -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) => {
|
||||||
return Parse.Promise.as();
|
resolve({
|
||||||
|
error: err,
|
||||||
|
response: res,
|
||||||
|
payload: notification,
|
||||||
|
deviceType: 'ios'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export class ParsePushAdapter extends PushAdapter {
|
|||||||
immediatePush: true
|
immediatePush: true
|
||||||
};
|
};
|
||||||
let pushTypes = Object.keys(pushConfig);
|
let pushTypes = Object.keys(pushConfig);
|
||||||
|
|
||||||
for (let pushType of pushTypes) {
|
for (let pushType of pushTypes) {
|
||||||
if (this.validPushTypes.indexOf(pushType) < 0) {
|
if (this.validPushTypes.indexOf(pushType) < 0) {
|
||||||
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
|
||||||
@@ -35,7 +35,7 @@ export class ParsePushAdapter extends PushAdapter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getValidPushTypes() {
|
getValidPushTypes() {
|
||||||
return this.validPushTypes;
|
return this.validPushTypes;
|
||||||
}
|
}
|
||||||
@@ -43,7 +43,7 @@ export class ParsePushAdapter extends PushAdapter {
|
|||||||
static classifyInstallations(installations, validTypes) {
|
static classifyInstallations(installations, validTypes) {
|
||||||
return classifyInstallations(installations, validTypes)
|
return classifyInstallations(installations, validTypes)
|
||||||
}
|
}
|
||||||
|
|
||||||
send(data, installations) {
|
send(data, installations) {
|
||||||
let deviceMap = classifyInstallations(installations, this.validPushTypes);
|
let deviceMap = classifyInstallations(installations, this.validPushTypes);
|
||||||
let sendPromises = [];
|
let sendPromises = [];
|
||||||
|
|||||||
@@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user