0

For some reason Badge Numbers are not being incremented in my app

I tried

[UIApplication sharedApplication].applicationIconBadgeNumber =
               [UIApplication sharedApplication].applicationIconBadgeNumber + 1;

and also:

[UIApplication sharedApplication].applicationIconBadgeNumber =
          [UIApplication sharedApplication].applicationIconBadgeNumber + 
               [[[userInfo objectForKey:@"aps"] objectForKey: @"badge"] intValue];

Full Code:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [UIApplication sharedApplication].applicationIconBadgeNumber =
              [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
}

It always displays 1, from my server I send the playload with badges = 1

2 Answers2

1

You should keep track of badge counters on the server.

When your app become active, set the badge count to 0 [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

and send a request to a custom API of your server that's telling that the badge count is 0 now

Maksim Usenko
  • 311
  • 3
  • 5
  • I have that in my code already , no problem there, the badge even shows, its just not incremented –  Mar 28 '14 at 20:49
  • Your server should sent push notification with badge count. After that, when your app become active, you should set app badge to zero and sent request to your server with badge count:0. I've implemented so. – Maksim Usenko Mar 28 '14 at 21:07
  • yeah i realized that it would increase only if the app is active, so basically you are saying I should be tracking the badge number by storing that into my database, that sounds like more complicated than it should be –  Mar 28 '14 at 21:11
  • You should not store the badge number in your database. The server increases the badge count after every send push and your app badge count will increase. And when the app became active, you have to set the badge count to zero and make request to the server with the badge count 0. And next push will come with the badge count 1. – Maksim Usenko Mar 28 '14 at 21:30
  • Im sure what you say makes sense, Im just not able to follow your explanation. You say send request to the server with badge count or 0 . Could you please edit your answer to elaborate more. thanks –  Mar 28 '14 at 21:37
  • I mean how do I keep track of it on my server without using a database? –  Mar 28 '14 at 21:45
  • You sent to the server only '0'! The server store your badge count Let's try step by step 1. Your app not active 2. The server sent push to your app 3. Your app shows the badge count equal to 1 4. The server sent the push again with badge count equal to 2 5. Your app shows the badge count equal to 2 6. And when your app became active, you should set your app badge count to 0. And make request to the server where you tell that badge count is equal to 0. – Maksim Usenko Mar 28 '14 at 21:51
  • 1
    You do need a database on the server to keep track of badge counters. And I think that by sending badge count 0 to server Maksim means sending a request to a custom API of your server that's telling that the badge count is 0 now. – Andris Zalitis Mar 28 '14 at 21:52
  • Al Pacino, I hope we helped you. – Maksim Usenko Mar 28 '14 at 22:04
  • @AndrisZalitis thanks now I understood. It wasnt so clear before. If Maksim is willing to edit his answer, Ill be glad to accept it –  Mar 28 '14 at 22:34
0

The problem is didReceiveRemoteNotification does not get called when your app is not active. Therefore you can only increase it once the application becomes active.

You will need to track the bagde count in your database and send it along with the push notification.

Now your app will be responsible for decreasing/increasing that count and finally update that badge count field into your table so that on the next push alerts your server knows what badge number the app icon should display.

meda
  • 45,103
  • 14
  • 92
  • 122