views:

975

answers:

2

I've been implementing the push service to my application, and I've been thinking about the application's badge. My app is a mail app (sorta) and I want to notify the user via push for new messages added to the inbox, I want the badge = number of new messages in the inbox.

I thought of doing it server sided (provider) checking for new messages and sending the number as the badge.

The question is: Is there a way to auto-increment the application's badge, without having to calculate the badge value server sided and afterwards sending it as a part of the push payload to the APSN?

Maybe there's a way to send in JSON badge field some variable like "++" or something like that. Any hack for that? Or do I need to go with the counting system server-sided??

+6  A: 

Nope, you'll have to track this on the server side. If you don't include any value for badge, it will get removed completely.

Of course though this is only if the user receives the notification and the app isn't running/they choose not to launch it. If the user launches the app or already had it running you can do whatever you want in regards to incrementing.

bpapa
That's what I thought.I already thought of a system, but it crossed my mind that Apple might have been a bit smarter this time - and thought of it themselves.Thanks for clearing that up.
natanavra
A: 

It's sort of possible but there's a trade-off.

You can always send up the unread total as an add-on JSON value as part of the push payload (push ignores keys it doesn't explicitly understand). Once the user opens the app, read the value and adjust the badge programmatically yourself via UIApplication's applicationIconBadgeNumber property.

The problem with doing it that way is that push adjusts the badge value even if the user doesn't open the app (i.e. when they get the notice and the user hits 'Cancel' instead of 'View'). In those cases your badge won't change, but as soon as they run the app (if they hit 'View') then your app can set it right.

Ramin
It's not really an answer... you said I can add it as JSON's payload, but that's something I can already do, I meant - is there a way to auto-increment the badge with each push...The answer, it appears - is no.I will have to manage it on the server, and then send a number as aps=>badge...Reseting the badge myself whenever the user gets into the application, and also reseting the counter on the server whenever the user fetches data.Thanks anyways.
natanavra