Skip to content

Commit

Permalink
Merge pull request #1248 from ParsePlatform/flovilmart.incrementBadge
Browse files Browse the repository at this point in the history
Supports increment as well as Increment
  • Loading branch information
drew-gross committed Mar 29, 2016
2 parents ef08dcf + cb4b036 commit 337d3c2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 34 deletions.
94 changes: 62 additions & 32 deletions spec/Parse.Push.spec.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
'use strict';


describe('Parse.Push', () => {
it('should properly send push', (done) => {
var pushAdapter = {
send: function(body, installations) {
var badge = body.data.badge;
let promises = installations.map((installation) => {
if (installation.deviceType == "ios") {
expect(installation.badge).toEqual(badge);
expect(installation.originalBadge+1).toEqual(installation.badge);
} else {
expect(installation.badge).toBeUndefined();
}
return Promise.resolve({
err: null,
deviceType: installation.deviceType,
result: true
})
});
return Promise.all(promises)
},
getValidPushTypes: function() {
return ["ios", "android"];
}

var setup = function() {
var pushAdapter = {
send: function(body, installations) {
var badge = body.data.badge;
let promises = installations.map((installation) => {
if (installation.deviceType == "ios") {
expect(installation.badge).toEqual(badge);
expect(installation.originalBadge+1).toEqual(installation.badge);
} else {
expect(installation.badge).toBeUndefined();
}
return Promise.resolve({
err: null,
deviceType: installation.deviceType,
result: true
})
});
return Promise.all(promises);
},
getValidPushTypes: function() {
return ["ios", "android"];
}
}

setServerConfiguration({
appId: Parse.applicationId,
masterKey: Parse.masterKey,
Expand All @@ -31,6 +35,7 @@ describe('Parse.Push', () => {
adapter: pushAdapter
}
});

var installations = [];
while(installations.length != 10) {
var installation = new Parse.Object("_Installation");
Expand All @@ -41,21 +46,46 @@ describe('Parse.Push', () => {
installation.set("deviceType", "ios");
installations.push(installation);
}
Parse.Object.saveAll(installations).then(() => {
return Parse.Object.saveAll(installations);
}

it('should properly send push', (done) => {
return setup().then(() => {
return Parse.Push.send({
where: {
deviceType: 'ios'
},
data: {
badge: 'Increment',
alert: 'Hello world!'
}
}, {useMasterKey: true});
where: {
deviceType: 'ios'
},
data: {
badge: 'Increment',
alert: 'Hello world!'
}
}, {useMasterKey: true})
})
.then(() => {
done();
}, (err) => {
console.error(err);
console.error();
fail('should not fail sending push')
done();
});
});

it('should properly send push with lowercaseIncrement', (done) => {
return setup().then(() => {
return Parse.Push.send({
where: {
deviceType: 'ios'
},
data: {
badge: 'increment',
alert: 'Hello world!'
}
}, {useMasterKey: true})
}).then(() => {
done();
}, (err) => {
console.error();
fail('should not fail sending push')
done();
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/Controllers/PushController.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class PushController extends AdaptableController {
if (body.data && body.data.badge) {
let badge = body.data.badge;
let op = {};
if (badge == "Increment") {
if (typeof badge == 'string' && badge.toLowerCase() === 'increment') {
op = { $inc: { badge: 1 } }
} else if (Number(badge)) {
op = { $set: { badge: badge } }
Expand Down Expand Up @@ -97,7 +97,7 @@ export class PushController extends AdaptableController {
}

sendToAdapter(body, installations, pushStatus, config) {
if (body.data && body.data.badge && body.data.badge == "Increment") {
if (body.data && body.data.badge && typeof body.data.badge == 'string' && body.data.badge.toLowerCase() == "increment") {
// Collect the badges to reduce the # of calls
let badgeInstallationsMap = installations.reduce((map, installation) => {
let badge = installation.badge;
Expand Down

0 comments on commit 337d3c2

Please sign in to comment.