Skip to content

Commit 5370fd9

Browse files
committed
Fixes problems related to increment badge
- name conventions are aweful in PushController - properly looks at the badge into body.data instead of body - We may want to refactor that as it's confusing to use a full body
1 parent f1f9bde commit 5370fd9

File tree

4 files changed

+78
-24
lines changed

4 files changed

+78
-24
lines changed

spec/Parse.Push.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
describe('Parse.Push', () => {
2+
it('should properly send push', (done) => {
3+
var pushAdapter = {
4+
send: function(body, installations) {
5+
return Promise.resolve({
6+
body: body,
7+
installations: installations
8+
})
9+
},
10+
getValidPushTypes: function() {
11+
return ["ios", "android"];
12+
}
13+
}
14+
setServerConfiguration({
15+
appId: Parse.applicationId,
16+
masterKey: Parse.masterKey,
17+
serverURL: Parse.serverURL,
18+
push: {
19+
adapter: pushAdapter
20+
}
21+
});
22+
var installations = [];
23+
while(installations.length != 10) {
24+
var installation = new Parse.Object("_Installation");
25+
installation.set("installationId", "installation_"+installations.length);
26+
installation.set("deviceToken","device_token_"+installations.length)
27+
installation.set("badge", installations.length);
28+
installation.set("originalBadge", installations.length);
29+
installation.set("deviceType", "ios");
30+
installations.push(installation);
31+
}
32+
Parse.Object.saveAll(installations).then(() => {
33+
return Parse.Push.send({
34+
where: {
35+
deviceType: 'ios'
36+
},
37+
data: {
38+
badge: 'Increment',
39+
alert: 'Hello world!'
40+
}
41+
}, {useMasterKey: true});
42+
})
43+
.then(() => {
44+
console.log("OK!");
45+
done();
46+
}, (err) => {
47+
console.error(err);
48+
done();
49+
});
50+
});
51+
});

spec/PushController.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ describe('PushController', () => {
132132

133133
it('properly increment badges', (done) => {
134134

135-
var payload = {
135+
var payload = {data:{
136136
alert: "Hello World!",
137137
badge: "Increment",
138-
}
138+
}}
139139
var installations = [];
140140
while(installations.length != 10) {
141141
var installation = new Parse.Object("_Installation");
@@ -157,7 +157,7 @@ describe('PushController', () => {
157157

158158
var pushAdapter = {
159159
send: function(body, installations) {
160-
var badge = body.badge;
160+
var badge = body.data.badge;
161161
installations.forEach((installation) => {
162162
if (installation.deviceType == "ios") {
163163
expect(installation.badge).toEqual(badge);
@@ -196,10 +196,10 @@ describe('PushController', () => {
196196

197197
it('properly set badges to 1', (done) => {
198198

199-
var payload = {
199+
var payload = {data: {
200200
alert: "Hello World!",
201201
badge: 1,
202-
}
202+
}}
203203
var installations = [];
204204
while(installations.length != 10) {
205205
var installation = new Parse.Object("_Installation");
@@ -213,7 +213,7 @@ describe('PushController', () => {
213213

214214
var pushAdapter = {
215215
send: function(body, installations) {
216-
var badge = body.badge;
216+
var badge = body.data.badge;
217217
installations.forEach((installation) => {
218218
expect(installation.badge).toEqual(badge);
219219
expect(1).toEqual(installation.badge);
@@ -244,6 +244,6 @@ describe('PushController', () => {
244244
done();
245245
});
246246

247-
})
247+
});
248248

249249
});

src/Controllers/PushController.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,16 @@ export class PushController extends AdaptableController {
6060
body['expiration_time'] = PushController.getExpirationTime(body);
6161
// TODO: If the req can pass the checking, we return immediately instead of waiting
6262
// pushes to be sent. We probably change this behaviour in the future.
63-
let badgeUpdate = Promise.resolve();
63+
let badgeUpdate = () => {
64+
return Promise.resolve();
65+
}
6466

65-
if (body.badge) {
67+
if (body.data && body.data.badge) {
6668
var op = {};
67-
if (body.badge == "Increment") {
69+
if (body.data.badge == "Increment") {
6870
op = {'$inc': {'badge': 1}}
69-
} else if (Number(body.badge)) {
70-
op = {'$set': {'badge': body.badge } }
71+
} else if (Number(body.data.badge)) {
72+
op = {'$set': {'badge': body.data.badge } }
7173
} else {
7274
throw "Invalid value for badge, expected number or 'Increment'";
7375
}
@@ -77,33 +79,35 @@ export class PushController extends AdaptableController {
7779
updateWhere.deviceType = 'ios';
7880

7981
// TODO: @nlutsenko replace with better thing
80-
badgeUpdate = config.database.rawCollection("_Installation").then((coll) => {
81-
return coll.update(updateWhere, op, { multi: true });
82-
});
82+
badgeUpdate = () => {
83+
return config.database.rawCollection("_Installation").then((coll) => {
84+
return coll.update(updateWhere, op, { multi: true });
85+
});
86+
}
8387
}
8488

85-
return badgeUpdate.then(() => {
86-
return rest.find(config, auth, '_Installation', where)
89+
return badgeUpdate().then(() => {
90+
return rest.find(config, auth, '_Installation', where);
8791
}).then((response) => {
88-
if (body.badge && body.badge == "Increment") {
92+
if (body.data && body.data.badge && body.data.badge == "Increment") {
8993
// Collect the badges to reduce the # of calls
9094
let badgeInstallationsMap = response.results.reduce((map, installation) => {
9195
let badge = installation.badge;
9296
if (installation.deviceType != "ios") {
9397
badge = UNSUPPORTED_BADGE_KEY;
9498
}
95-
map[badge] = map[badge] || [];
96-
map[badge].push(installation);
99+
map[badge+''] = map[badge+''] || [];
100+
map[badge+''].push(installation);
97101
return map;
98102
}, {});
99-
103+
100104
// Map the on the badges count and return the send result
101105
let promises = Object.keys(badgeInstallationsMap).map((badge) => {
102106
let payload = deepcopy(body);
103107
if (badge == UNSUPPORTED_BADGE_KEY) {
104-
delete payload.badge;
108+
delete payload.data.badge;
105109
} else {
106-
payload.badge = parseInt(badge);
110+
payload.data.badge = parseInt(badge);
107111
}
108112
return pushAdapter.send(payload, badgeInstallationsMap[badge]);
109113
});

src/Routers/PushRouter.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export class PushRouter extends PromiseRouter {
2929
}
3030

3131
var where = PushRouter.getQueryCondition(req);
32-
3332
pushController.sendPush(req.body, where, req.config, req.auth);
3433
return Promise.resolve({
3534
response: {

0 commit comments

Comments
 (0)