5

I am working on push notification in iphone. when i receive push notification, its showing 1 on my application icon, next time its 2,3,4. if i open application its 0. Next time its should be 1,2,3,4... but its showing last number and +1. i want reset push notification badge after open application. i am sending +1 from urban airship.

and its not working for me.

 [[UIApplication sharedApplication] cancelAllLocalNotifications];
 [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
iOS Developer
  • 103
  • 1
  • 8
  • You are clearing the badge number locally. You also need to clear it from Urban Airship server. I don't know Urban Airship. But I am sure it has an API to reset the notification badge at the server. – EmptyStack Dec 07 '12 at 05:13
  • i am sending badge number +1 for increment in my api. – iOS Developer Dec 07 '12 at 05:15

2 Answers2

3

I use this code in my app because the Urban Airship (UA) documentation said to

[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
[[UAPush shared] resetBadge];

but it doesn't work, the badge on the app icon keeps incrementing. I saw a few posts on this very issue on UA's forums and they haven't given a clear answer.

EDIT #1:

I received the following response from a support technician at UA with the following suggestions, which worked great:

What you want to do, is ensure that in your didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method, you are calling the following:

[[UAPush shared] setAutobadgeEnabled:YES];
[[UAPush shared] resetBadge];//zero badge on startup

and also call [[UAPush shared] resetBadge]; in the following methods as well:

applicationDidBecomeActive:(UIApplication *)application

and

didReceiveRemoteNotification:(NSDictionary *)userInfo

The technician also mentioned that assigning 0 to applicationIconBadgeNumber is not necessary, so I took it out. Still works beautifully.

EDIT #2:

I ended up having to modify application:didReceiveRemoteNotification: to include a call to UA's handleNotification:applicationState: method:

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    // Get application state for iOS4.x+ devices, otherwise assume active
    UIApplicationState appState = UIApplicationStateActive;
    if ([application respondsToSelector:@selector(applicationState)])
    {
        appState = application.applicationState;
    } 

    [[UAPush shared] handleNotification:userInfo applicationState:appState];
    [[UAPush shared] resetBadge];
}

because I was having a problem with the following scenario:

  1. User is in app
  2. Push notification received
  3. No badge was displayed on app icon when returning to home screen (as expected)
  4. Another push notification is received
  5. Badge displayed number greater than 1

With the modification above, this scenario is handled. I guess you have to tell UA that the notification is handled when one is received and the app is running in the foreground.

Moebius
  • 700
  • 7
  • 25
  • I am having the same problem. I tried your suggestions, but it isn't working for me. Can you please help me fix it? Please take a look at my question here: http://stackoverflow.com/questions/20073212/reset-urbanairship-badge-count – Adil Malik Nov 19 '13 at 14:20
  • I'm not sure what other information I can add. The code you see above works for me. – Moebius Nov 19 '13 at 18:32
0

I have used Urban Airship and have had this problem before. You are not showing the code but i am assuming that when a notification is received, you are setting the application badge number to what urban airship is passing to you, don't do that. Just let the application self handle this, when a remote notification comes in, let it auto increment on its own. If that is not the case it could be that, on the urban airship side, you are setting a badge number to send with the push. Do not send a badge number with the push notification, leave that part out, iOS should auto increment your badge from its current badge number.

Kris Gellci
  • 9,539
  • 6
  • 40
  • 47