0

So I have push notifications working perfectly except that the badge notification is only ever going up to 3 and then, even though it shows those notifications, the badge number doesn't go above that. Here is badge register and clear code on launch to clear the badge:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
 (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

I have no other code setting the badge.

I'll be happy to answer any question, but I need to get this fixed^^; Thanks in advance for any help!

Edits in italics.

Eran
  • 387,369
  • 54
  • 702
  • 768
Matthew Clark
  • 571
  • 1
  • 9
  • 33
  • Are you sure you are sending push notifications with badge number > 3? – Eran Jul 19 '13 at 17:40
  • If you only set the badge number in the code you supplied, it would always stay 0. Since you see it going up to 3, you are probably sending it from the server in the `badge` parameter of the push notifications. – Eran Jul 19 '13 at 21:08
  • @Eran that was the case. I thought would inc. on it's own and thought that was the type of badge (so I left it at 3). Where should I be inc. that count? application: didReceiveRemoteNotification:? – Matthew Clark Jul 19 '13 at 21:18
  • You should send it from your server. See my answer. – Eran Jul 19 '13 at 23:36

2 Answers2

1

If you intend to update the badge count as a result of arriving push notifications, you should probably set its value in the server (using the badge property of the push notification).

When your server sends push notifications to your app they are not guaranteed to arrive. If you send several notifications in a short time period, it's possible that Apple will deliver only some of them. Therefore it's not a good practice to increase the badge count in the app when a notification arrives.

Another reason why it's not a good practice is that if you update the badge programatically in the app, the update will take place only when the user taps the notification (and if the user opens the application via the launch icon, you wouldn't even know there was a notification, so you won't know to update the badge).

The idea of the badge is that the server sends it as part of the push notification. Then iOS displays the badge on the app icon even if you don't tap the notification. This tells the user he/she has new information to view in the app. When the app is launched, you should display the new data and clear the badge (by setting its value to 0).

Usually the badge number returned by the server contains the number of new data items available at the server for the user of the app (such as the number of unread emails). Letting the server maintain this number makes even more sense when the user can access the data from the server via multiple devices. If you read all your new emails on your laptop, you don't want your email app to show a badge number that indicates unread messages.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

Be aware that setApplicationIconBadgeNumber is not cumulative. It will not do the arithmetic for you. You need to set the badge number to the exact value you want. So if you wish to tally up the notifications, you will need to check the current value of applicationIconBadgeNumber and then add 1 to it for each notification.

Sending 0 means "no change" when in the context of a UILocalNotification and means "hide" when in the context of UIApplication.

Ric Perrott
  • 597
  • 2
  • 9
  • Sorry I guess I wasn't clear. That code is in application: didFinishLaunchingWithOptions: and is the only place that I change the badge number on the app side. I'll have to look back at the server code, but I don't think I set it there (if I even can). – Matthew Clark Jul 19 '13 at 20:57
  • The most common pattern is to have your application make a call to your server when it enters the background telling the server what the current state of the `applciationIconBadgeNumber` is. That way whenever your server sends a new notification, it can properly send the cumulative total of unread notifications and iOS will set the app icon badge appropriately. When the app launches, or your user checks their notifications, then you can simply set the badge number to 0, and call back to the server to tell it that there are no current unread notifications. Good Luck! – Ric Perrott Jul 22 '13 at 13:23