Skip to content

Commit bc3d279

Browse files
committed
Merge pull request #3 from parse-server-modules/reportAllErrors
reports all sending error for mis-configurations
2 parents 9dc570a + a19a26a commit bc3d279

File tree

4 files changed

+87
-9
lines changed

4 files changed

+87
-9
lines changed

spec/ParsePushAdapter.spec.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,65 @@ describe('ParsePushAdapter', () => {
141141
done();
142142
});
143143

144+
it('reports properly results', (done) => {
145+
var pushConfig = {
146+
android: {
147+
senderId: 'senderId',
148+
apiKey: 'apiKey'
149+
},
150+
ios: [
151+
{
152+
cert: 'cert.cer',
153+
key: 'key.pem',
154+
production: false,
155+
bundleId: 'bundleId'
156+
}
157+
]
158+
};
159+
var installations = [
160+
{
161+
deviceType: 'android',
162+
deviceToken: 'androidToken'
163+
},
164+
{
165+
deviceType: 'ios',
166+
deviceToken: 'c5ee8fae0a1c',
167+
appIdentifier: 'anotherBundle'
168+
},
169+
{
170+
deviceType: 'ios',
171+
deviceToken: 'c5ee8fae0a1c5805e731cf15496d5b2b3f9b9c577353d3239429d3faaee01c79',
172+
appIdentifier: 'anotherBundle'
173+
},
174+
{
175+
deviceType: 'win',
176+
deviceToken: 'winToken'
177+
},
178+
{
179+
deviceType: 'android',
180+
deviceToken: undefined
181+
}
182+
];
183+
184+
var parsePushAdapter = new ParsePushAdapter(pushConfig);
185+
parsePushAdapter.send({data: {alert: 'some'}}, installations).then((results) => {
186+
expect(Array.isArray(results)).toBe(true);
187+
188+
// 2x iOS, 1x android
189+
expect(results.length).toBe(3);
190+
results.forEach((result) => {
191+
expect(result.transmitted).toBe(false);
192+
expect(typeof result.device).toBe('object');
193+
expect(typeof result.device.deviceType).toBe('string');
194+
expect(typeof result.device.deviceToken).toBe('string');
195+
})
196+
done();
197+
}).catch((err) => {
198+
fail('Should not fail');
199+
done();
200+
})
201+
});
202+
144203
function makeDevice(deviceToken, appIdentifier) {
145204
return {
146205
deviceToken: deviceToken,

src/APNS.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ function APNS(args) {
7070
device.callback({
7171
notification: notification,
7272
transmitted: true,
73-
device: device
73+
device: {
74+
deviceType: 'ios',
75+
deviceToken: device.token.toString('hex')
76+
}
7477
});
7578
}
7679
console.log('APNS Connection %d Notification transmitted to %s', conn.index, device.token.toString('hex'));
@@ -105,6 +108,10 @@ APNS.prototype.send = function(data, devices) {
105108
if (qualifiedConnIndexs.length == 0) {
106109
return Promise.resolve({
107110
transmitted: false,
111+
device: {
112+
deviceToken: device.deviceToken,
113+
deviceType: 'ios'
114+
},
108115
result: {error: 'No connection available'}
109116
});
110117
}
@@ -148,9 +155,13 @@ function handleTransmissionError(conns, errCode, notification, apnDevice) {
148155
if (newConnIndex < 0 || newConnIndex >= conns.length) {
149156
if (apnDevice.callback) {
150157
apnDevice.callback({
151-
response: {error: `APNS can not find vaild connection for ${apnDevice.token}`, code: errCode},
158+
response: {error: `APNS can not find vaild connection for ${apnDevice.token.toString('hex')}`, code: errCode},
152159
status: errCode,
153-
transmitted: false
160+
transmitted: false,
161+
device: {
162+
deviceType: 'ios',
163+
deviceToken: apnDevice.token.toString('hex')
164+
}
154165
});
155166
}
156167
return;

src/GCM.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ GCM.prototype.send = function(data, devices) {
8585
let promise = promises[index];
8686
let result = results ? results[index] : undefined;
8787
let device = devicesMap[token];
88+
device.deviceType = 'android';
8889
let resolution = {
8990
device,
9091
multicast_id,

src/ParsePushAdapter.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,24 @@ export class ParsePushAdapter {
4646
let sendPromises = [];
4747
for (let pushType in deviceMap) {
4848
let sender = this.senderMap[pushType];
49+
let devices = deviceMap[pushType];
4950
if (!sender) {
50-
sendPromises.push(Promise.resolve({
51-
transmitted: false,
52-
response: {'error': `Can not find sender for push type ${pushType}, ${data}`}
53-
}))
51+
let results = devices.map((device) => {
52+
return Promise.resolve({
53+
device,
54+
transmitted: false,
55+
response: {'error': `Can not find sender for push type ${pushType}, ${data}`}
56+
})
57+
});
58+
sendPromises.push(Promise.all(results));
5459
} else {
55-
let devices = deviceMap[pushType];
5660
sendPromises.push(sender.send(data, devices));
5761
}
5862
}
59-
return Parse.Promise.when(sendPromises);
63+
return Promise.all(sendPromises).then((promises) => {
64+
// flatten all
65+
return [].concat.apply([], promises);
66+
})
6067
}
6168
}
6269
export default ParsePushAdapter;

0 commit comments

Comments
 (0)