Skip to content

Commit 98769a2

Browse files
committed
Merge pull request #902 from ParsePlatform/flovilmart.FixPushNotifications
Increment badge the right way
2 parents 8086974 + bf96f0d commit 98769a2

File tree

3 files changed

+87
-23
lines changed

3 files changed

+87
-23
lines changed

spec/Parse.Push.spec.js

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

spec/PushController.spec.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ describe('PushController', () => {
107107

108108
it('properly increment badges', (done) => {
109109

110-
var payload = {
110+
var payload = {data:{
111111
alert: "Hello World!",
112112
badge: "Increment",
113-
}
113+
}}
114114
var installations = [];
115115
while(installations.length != 10) {
116116
var installation = new Parse.Object("_Installation");
@@ -132,7 +132,7 @@ describe('PushController', () => {
132132

133133
var pushAdapter = {
134134
send: function(body, installations) {
135-
var badge = body.badge;
135+
var badge = body.data.badge;
136136
installations.forEach((installation) => {
137137
if (installation.deviceType == "ios") {
138138
expect(installation.badge).toEqual(badge);
@@ -171,10 +171,10 @@ describe('PushController', () => {
171171

172172
it('properly set badges to 1', (done) => {
173173

174-
var payload = {
174+
var payload = {data: {
175175
alert: "Hello World!",
176176
badge: 1,
177-
}
177+
}}
178178
var installations = [];
179179
while(installations.length != 10) {
180180
var installation = new Parse.Object("_Installation");
@@ -188,7 +188,7 @@ describe('PushController', () => {
188188

189189
var pushAdapter = {
190190
send: function(body, installations) {
191-
var badge = body.badge;
191+
var badge = body.data.badge;
192192
installations.forEach((installation) => {
193193
expect(installation.badge).toEqual(badge);
194194
expect(1).toEqual(installation.badge);
@@ -219,6 +219,6 @@ describe('PushController', () => {
219219
done();
220220
});
221221

222-
})
222+
});
223223

224224
});

src/Controllers/PushController.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -48,55 +48,60 @@ export class PushController extends AdaptableController {
4848
body['expiration_time'] = PushController.getExpirationTime(body);
4949
// TODO: If the req can pass the checking, we return immediately instead of waiting
5050
// pushes to be sent. We probably change this behaviour in the future.
51-
let badgeUpdate = Promise.resolve();
51+
let badgeUpdate = () => {
52+
return Promise.resolve();
53+
}
5254

53-
if (body.badge) {
55+
if (body.data && body.data.badge) {
56+
let badge = body.data.badge;
5457
let op = {};
55-
if (body.badge == "Increment") {
58+
if (badge == "Increment") {
5659
op = { $inc: { badge: 1 } }
57-
} else if (Number(body.badge)) {
58-
op = { $set: { badge: body.badge } }
60+
} else if (Number(badge)) {
61+
op = { $set: { badge: badge } }
5962
} else {
6063
throw "Invalid value for badge, expected number or 'Increment'";
6164
}
6265
let updateWhere = deepcopy(where);
6366
updateWhere.deviceType = 'ios'; // Only on iOS!
6467

65-
badgeUpdate = config.database.adaptiveCollection("_Installation")
68+
badgeUpdate = () => {
69+
return config.database.adaptiveCollection("_Installation")
6670
.then(coll => coll.updateMany(updateWhere, op));
71+
}
6772
}
6873

69-
return badgeUpdate.then(() => {
70-
return rest.find(config, auth, '_Installation', where)
74+
return badgeUpdate().then(() => {
75+
return rest.find(config, auth, '_Installation', where);
7176
}).then((response) => {
72-
if (body.badge && body.badge == "Increment") {
77+
if (body.data && body.data.badge && body.data.badge == "Increment") {
7378
// Collect the badges to reduce the # of calls
7479
let badgeInstallationsMap = response.results.reduce((map, installation) => {
7580
let badge = installation.badge;
7681
if (installation.deviceType != "ios") {
7782
badge = UNSUPPORTED_BADGE_KEY;
7883
}
79-
map[badge] = map[badge] || [];
80-
map[badge].push(installation);
84+
map[badge+''] = map[badge+''] || [];
85+
map[badge+''].push(installation);
8186
return map;
8287
}, {});
83-
88+
8489
// Map the on the badges count and return the send result
8590
let promises = Object.keys(badgeInstallationsMap).map((badge) => {
8691
let payload = deepcopy(body);
8792
if (badge == UNSUPPORTED_BADGE_KEY) {
88-
delete payload.badge;
93+
delete payload.data.badge;
8994
} else {
90-
payload.badge = parseInt(badge);
95+
payload.data.badge = parseInt(badge);
9196
}
92-
return pushAdapter.send(payload, badgeInstallationsMap[badge]);
97+
return pushAdapter.send(payload, badgeInstallationsMap[badge]);
9398
});
9499
return Promise.all(promises);
95100
}
96101
return pushAdapter.send(body, response.results);
97102
});
98103
}
99-
104+
100105
/**
101106
* Get expiration time from the request body.
102107
* @param {Object} request A request object

0 commit comments

Comments
 (0)