-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Adds support for badging on iOS #740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,11 @@ import PromiseRouter from '../PromiseRouter'; | |
import rest from '../rest'; | ||
import AdaptableController from './AdaptableController'; | ||
import { PushAdapter } from '../Adapters/Push/PushAdapter'; | ||
import deepcopy from 'deepcopy'; | ||
import features from '../features'; | ||
|
||
const FEATURE_NAME = 'push'; | ||
const UNSUPPORTED_BADGE_KEY = "unsupported"; | ||
|
||
export class PushController extends AdaptableController { | ||
|
||
|
@@ -58,7 +60,55 @@ export class PushController extends AdaptableController { | |
body['expiration_time'] = PushController.getExpirationTime(body); | ||
// TODO: If the req can pass the checking, we return immediately instead of waiting | ||
// pushes to be sent. We probably change this behaviour in the future. | ||
rest.find(config, auth, '_Installation', where).then(function(response) { | ||
let badgeUpdate = Promise.resolve(); | ||
|
||
if (body.badge) { | ||
var op = {}; | ||
if (body.badge == "Increment") { | ||
op = {'$inc': {'badge': 1}} | ||
} else if (Number(body.badge)) { | ||
op = {'$set': {'badge': body.badge } } | ||
} else { | ||
throw "Invalid value for badge, expected number or 'Increment'"; | ||
} | ||
let updateWhere = deepcopy(where); | ||
|
||
// Only on iOS! | ||
updateWhere.deviceType = 'ios'; | ||
|
||
// TODO: @nlutsenko replace with better thing | ||
badgeUpdate = config.database.rawCollection("_Installation").then((coll) => { | ||
return coll.update(updateWhere, op, { multi: true }); | ||
}); | ||
} | ||
|
||
return badgeUpdate.then(() => { | ||
return rest.find(config, auth, '_Installation', where) | ||
}).then((response) => { | ||
if (body.badge && body.badge == "Increment") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks fine, what do you think if we would inplace mutate every installation instead before sending? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, would actually remove the requirement to send multiple times, since the complexity of sending with a badge and without is the same. |
||
// Collect the badges to reduce the # of calls | ||
let badgeInstallationsMap = response.results.reduce((map, installation) => { | ||
let badge = installation.badge; | ||
if (installation.deviceType != "ios") { | ||
badge = UNSUPPORTED_BADGE_KEY; | ||
} | ||
map[badge] = map[badge] || []; | ||
map[badge].push(installation); | ||
return map; | ||
}, {}); | ||
|
||
// Map the on the badges count and return the send result | ||
let promises = Object.keys(badgeInstallationsMap).map((badge) => { | ||
let payload = deepcopy(body); | ||
if (badge == UNSUPPORTED_BADGE_KEY) { | ||
delete payload.badge; | ||
} else { | ||
payload.badge = parseInt(badge); | ||
} | ||
return pushAdapter.send(payload, badgeInstallationsMap[badge]); | ||
}); | ||
return Promise.all(promises); | ||
} | ||
return pushAdapter.send(body, response.results); | ||
}); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a way to reuse this code for Amazon SNS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You actually don't need to use this piece, since this happens before PushAdapter, which is the extension point where SNS would be plugged in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even better...thanks!