Skip to content

Commit a392c08

Browse files
committed
Uses the resolved promise from the adapter
1 parent 4d401d9 commit a392c08

File tree

5 files changed

+57
-23
lines changed

5 files changed

+57
-23
lines changed

spec/PushController.spec.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ describe('PushController', () => {
143143
}
144144
})
145145
return Promise.resolve({
146-
body: body,
147-
installations: installations
146+
error: null
147+
payload: body,
148148
})
149149
},
150150
getValidPushTypes: function() {
@@ -195,8 +195,8 @@ describe('PushController', () => {
195195
expect(1).toEqual(installation.badge);
196196
})
197197
return Promise.resolve({
198-
body: body,
199-
installations: installations
198+
payload: body,
199+
error: null
200200
})
201201
},
202202
getValidPushTypes: function() {
@@ -233,9 +233,10 @@ describe('PushController', () => {
233233
send: function(body, installations) {
234234
var badge = body.data.badge;
235235
return Promise.resolve({
236-
body: body,
237-
installations: installations
238-
})
236+
error: null,
237+
response: "OK!",
238+
payload: body
239+
});
239240
},
240241
getValidPushTypes: function() {
241242
return ["ios"];
@@ -271,7 +272,9 @@ describe('PushController', () => {
271272

272273
var pushAdapter = {
273274
send: function(body, installations) {
274-
return Promise.resolve();
275+
return Promise.resolve({
276+
error:null
277+
});
275278
},
276279
getValidPushTypes: function() {
277280
return ["ios"];

src/APNS.js

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ function APNS(args) {
6666
});
6767

6868
conn.on('transmitted', function(notification, device) {
69+
if (device.callback) {
70+
device.callback(null, {
71+
notification: notification,
72+
device: device
73+
});
74+
}
6975
console.log('APNS Connection %d Notification transmitted to %s', conn.index, device.token.toString('hex'));
7076
});
7177

@@ -91,11 +97,14 @@ APNS.prototype.send = function(data, devices) {
9197
let coreData = data.data;
9298
let expirationTime = data['expiration_time'];
9399
let notification = generateNotification(coreData, expirationTime);
94-
for (let device of devices) {
100+
101+
let promises = devices.map((device) => {
95102
let qualifiedConnIndexs = chooseConns(this.conns, device);
96103
// We can not find a valid conn, just ignore this device
97104
if (qualifiedConnIndexs.length == 0) {
98-
continue;
105+
return Promise.resolve({
106+
err: 'No connection available'
107+
});
99108
}
100109
let conn = this.conns[qualifiedConnIndexs[0]];
101110
let apnDevice = new apn.Device(device.deviceToken);
@@ -104,9 +113,19 @@ APNS.prototype.send = function(data, devices) {
104113
if (device.appIdentifier) {
105114
apnDevice.appIdentifier = device.appIdentifier;
106115
}
107-
conn.pushNotification(notification, apnDevice);
108-
}
109-
return Parse.Promise.as();
116+
return new Promise((resolve, reject) => {
117+
apnDevice.callback = (err, res) => {
118+
resolve({
119+
error: err,
120+
response: res,
121+
payload: notification,
122+
deviceType: 'ios'
123+
});
124+
}
125+
conn.pushNotification(notification, apnDevice);
126+
});
127+
});
128+
return Parse.Promise.when(promises);
110129
}
111130

112131
function handleTransmissionError(conns, errCode, notification, apnDevice) {
@@ -133,7 +152,12 @@ function handleTransmissionError(conns, errCode, notification, apnDevice) {
133152
}
134153
// There is no more available conns, we give up in this case
135154
if (newConnIndex < 0 || newConnIndex >= conns.length) {
136-
console.log('APNS can not find vaild connection for %j', apnDevice.token);
155+
if (apnDevice.callback) {
156+
apnDevice.callback({
157+
error: `APNS can not find vaild connection for ${apnDevice.token}`,
158+
code: errCode
159+
});
160+
}
137161
return;
138162
}
139163

src/Adapters/Push/ParsePushAdapter.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class ParsePushAdapter extends PushAdapter {
1919
immediatePush: true
2020
};
2121
let pushTypes = Object.keys(pushConfig);
22-
22+
2323
for (let pushType of pushTypes) {
2424
if (this.validPushTypes.indexOf(pushType) < 0) {
2525
throw new Parse.Error(Parse.Error.PUSH_MISCONFIGURED,
@@ -35,15 +35,15 @@ export class ParsePushAdapter extends PushAdapter {
3535
}
3636
}
3737
}
38-
38+
3939
getValidPushTypes() {
4040
return this.validPushTypes;
4141
}
4242

4343
static classifyInstallations(installations, validTypes) {
4444
return classifyInstallations(installations, validTypes)
4545
}
46-
46+
4747
send(data, installations) {
4848
let deviceMap = classifyInstallations(installations, this.validPushTypes);
4949
let sendPromises = [];

src/Controllers/PushController.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export class PushController extends AdaptableController {
9292
}).then(() => {
9393
return rest.find(config, auth, '_Installation', where);
9494
}).then((response) => {
95+
this.updatePushStatus({status: "running"}, {status:"pending", objectId: pushStatus.objectId}, config);
9596
if (body.data && body.data.badge && body.data.badge == "Increment") {
9697
// Collect the badges to reduce the # of calls
9798
let badgeInstallationsMap = response.results.reduce((map, installation) => {
@@ -117,8 +118,9 @@ export class PushController extends AdaptableController {
117118
return Promise.all(promises);
118119
}
119120
return pushAdapter.send(body, response.results, pushStatus);
120-
}).then(() => {
121-
return this.updatePushStatus({status: "running"}, pushStatus, config);
121+
}).then((results) => {
122+
console.log(results);
123+
return Promise.resolve(results);
122124
});
123125
}
124126

@@ -139,8 +141,8 @@ export class PushController extends AdaptableController {
139141
return restWrite.execute();
140142
}
141143

142-
updatePushStatus(update, pushStatus, config) {
143-
let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', {objectId: pushStatus.objectId, "status": "pending"}, update);
144+
updatePushStatus(update, where, config) {
145+
let restWrite = new RestWrite(config, {isMaster: true}, '_PushStatus', where, update);
144146
return restWrite.execute();
145147
}
146148

src/GCM.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function GCM(args) {
2121
* @param {Array} devices A array of devices
2222
* @returns {Object} A promise which is resolved after we get results from gcm
2323
*/
24-
GCM.prototype.send = function(data, devices) {
24+
GCM.prototype.send = function(data, devices, callback) {
2525
let pushId = cryptoUtils.newObjectId();
2626
let timeStamp = Date.now();
2727
let expirationTime;
@@ -52,7 +52,12 @@ GCM.prototype.send = function(data, devices) {
5252
request: message,
5353
response: response
5454
});
55-
sendPromise.resolve();
55+
sendPromise.resolve({
56+
error: error,
57+
response: response,
58+
payload: message,
59+
deviceType: 'android'
60+
});
5661
});
5762
sendPromises.push(sendPromise);
5863
}

0 commit comments

Comments
 (0)