0

in my application I have registered for remote notification. when a user publishes some content, every user receives a notification. I am trying to count the number of notifications that arrives and update the icon badge number.

If for example a user receives 3 notifications, the badge number must become 3, and when the user opens the app, the badge number goes back to 0. But I don't knw in which method to set the badge. I have tried this:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 2;    
// Handle Socialize notification at foreground
[self handleNotification:userInfo];
}

The problem with this is that the badge sets the number 2 when I actually open the app (bring it to foreground) if I have received notifications, and not when I receive the notification. How can I solve this? Thanks for any help.

EDIT:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:@"aps"] objectForKey: @"1"] intValue];
// Handle Socialize notification at foreground
[self handleNotification:userInfo];
}
user2014474
  • 1,117
  • 5
  • 19
  • 36
  • Are you getting the the badgecount from server or you count it locally? – Exploring Feb 16 '13 at 13:36
  • but i don't need a precise number, but just something to tell the user that there is something to see – user2014474 Feb 16 '13 at 13:44
  • @user2014474 If you don't need the exact number, set `"badge": 1` in your notification JSON payload. That way, whenever the user receives a notification, a "1" badge will appear on your app icon. In your app delegate use `application.applicationIconBadgeNumber = 0` to remove the badge. – Daniel Martín Feb 16 '13 at 13:47
  • Can you show me some sample code please? In which method do I set this? – user2014474 Feb 16 '13 at 13:48
  • The point is that i am using Socialize api, so I don't control the notifications and the way they present. Can I set this option in my app delegate? – user2014474 Feb 16 '13 at 13:49
  • @user2014474 http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW1 – Daniel Martín Feb 16 '13 at 13:50
  • See the edit. would it work? may be in stead of objectForKey:@"aps", objectForKey:@"badge" – user2014474 Feb 16 '13 at 13:54
  • @user2014474 No, the count must not be incremented in code because your app may not be running at all when it receives a notification. It's the payload you receive from the notification server what determines the number of pending notifications and badges your app icon. I don't know about the Socialize API, but it should keep the number of pending notifications for each user/device. – Daniel Martín Feb 16 '13 at 14:15

1 Answers1

3

You need to determine the badge count on your server before you send the notification. At the time the app received the notification it is too late to determine the correct number since the number from the notification immediately overwrites the number the app set.

Sven
  • 22,475
  • 4
  • 52
  • 71