28

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??

Jim Puls
  • 79,175
  • 10
  • 73
  • 78
natanavra
  • 2,100
  • 3
  • 18
  • 24
  • 2
    Is auto-increment still not possible? I'm building a chat app called [AcaniChat](https://github.com/acani/AcaniChat). Like iPhone's native Messages app, the badge count is equal to the number of new (unread) messages, and you get a push notification for each new message. So, if auto-increment were possible, I wouldn't have to store every device token's badge count on the server. – ma11hew28 Sep 16 '12 at 00:17

5 Answers5

30

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.

UPDATE March 2014: See comments for a possible update. I haven't done pushes in several years so haven't been able to verify this myself.

bpapa
  • 21,409
  • 25
  • 99
  • 147
  • 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 Dec 22 '09 at 19:24
  • 1
    Why not maintain badge number in app by caching it (e.g. NSUserDefaults) – msk Jun 12 '12 at 07:24
  • 2
    @MSK How does maintaining the badge number on the client solve this issue? Anyway, you could just do `[UIApplication sharedApplication].applicationIconBadgeNumber` if you wanted to get the badge number, no need to cache it in `NSUserDefaults`. – ma11hew28 Sep 16 '12 at 00:24
  • Now that I have already implemented it in our apps I know that I was wrong :) – msk Sep 16 '12 at 06:24
  • 10
    if badge is not included in the payload, it doesn't remove the badge from app icon. it just doesn't touch it. to remove badge, it has to be set to zero. – Mehmet Fatih Yıldız Nov 08 '13 at 20:05
  • If somebody else confirms what Mehmet says is the case (I haven't done anything with push notifications in quite some time) I'll edit my answer. That was how it worked in 2009. – bpapa Nov 09 '13 at 02:44
  • Mehmet is right about it not getting touched if badge is not present in the payload. I also tried with 0 and it still did not get touched. – Anand Mar 04 '14 at 12:29
  • 1
    @bpapa hi in my gmail app i dont open app but my icon badge set zero even i do not open app, Only i open the my pc browser and check the mail . how can it possible ? – Ilesh P Apr 30 '15 at 11:27
  • @llesh IDK! I haven't done Push Notifications in years. You should ask another question here. – bpapa Apr 30 '15 at 14:39
  • @MehmetFatihYıldız even if it's true that if you don't send a badge down it doesn't touch the badge count, how does this absolve you from keeping track of the count server side? If your app isn't in the foreground and you get a notification that doesn't touch the badge count, your badge count won't increment like it should. – Adam Johns May 15 '15 at 21:00
  • remember to typecast badge number to int on php side. – ios Feb 24 '16 at 10:22
2

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
  • 13,343
  • 3
  • 33
  • 35
  • 1
    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 Dec 22 '09 at 19:23
  • The other issue here is that by only updating the badge count after the user visits the app, we lose on the opportunity to engage less active users. – Zorayr Mar 08 '17 at 23:41
2

It is now possible to have the client auto-increment the badge using a UNNotificationServiceExtension.

Extensions have the ability to modify notification payloads before iOS displays them. In summary, store a badge counter in UserDefaults and modify the notification's badge count as needed. You'll need to add the App Groups capability to share User Defaults.

Here's a detailed step-by-step guide: https://prodocs.cometchat.com/docs/ios-increment-app-icon-badge-count

N S
  • 2,524
  • 6
  • 32
  • 42
1

You can try out App42 backend services which provide auto increment of push badge count which is maintained on the server side. For more details you can follow the link of blog. Here is the blogpost conent:

Here are the few use cases that can be achieved through auto incremental badge count in App42 Push Notification.

For auto increment of push badge by 1, you need to send push message as shown below.

PushNotificationService pushNotificationService = App42API.BuildPushNotificationService (); // Initializing PushNotification Service.
string userName = "UserName";
string message= "{'badge':'increment'}";
pushNotificationService.SendPushMessageToUser(userName,message, new UnityCallBack())

N.B: The sample explained is for Unity/C# but the same process can be applied on others too.

If you want to stipulate any number for the badge or want to reduce the badge count to zero, you can use this method to update the count as the notification gets clicked by the user. You have to call updatePushBadgeforDevice or updatePushBadgeforUser in this case.

PushNotificationService pushNotificationService = App42API.BuildPushNotificationService (); // Initializing PushNotification Service.
string userName = "UserName";
string deviceToken = "DeviceToken";
int badges = 10; // For clear count make it 0 
pushNotificationService.UpdatePushBadgeforDevice(userName, deviceToken, badges,  new UnityCallBack());

PushNotificationService pushNotificationService = App42API.BuildPushNotificationService (); // Initializing PushNotification Service.
string userName = "UserName";
int badges = 10; // For clear count make it 0
pushNotificationService.UpdatePushBadgeforUser(userName, badges,  new UnityCallBack());

updatePushBadgeforDevice – This method is used to update push badge count of a particular device registered by the user .

updatePushBadgeforUser – This method is used to update push badge count of all the devices that a user procures. In this case, we are assuming that the user has multiple devices registered under his name.

Alex Zavatone
  • 4,106
  • 36
  • 54
-17

Send +1 for badge count, which will auto-increment the badge count by 1.