Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear push notification badge after increment

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;
like image 230
iOS Developer Avatar asked Oct 21 '22 23:10

iOS Developer


1 Answers

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.

like image 115
Moebius Avatar answered Oct 27 '22 08:10

Moebius