williamsandonz
williamsandonz

Reputation: 16440

How to clear just the notification in iOS

If the user has alert style set to Banners. They can receive more than 1 notification without them being prompted to clear it. They then go to use their phone, and they have say 3 stored. If the click on the latest one & it opens the App, I want to only clear just this one notification, I also need to go badgeCount--;

How can I achieve it with the code below? (At the moment it's set to clearing all which I don't want...) I've also noticed that sometimes it DOES update the badge number. But if I toggle back to the iOS Home screen, and pull down the notifications menu, it's still there!

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    if([[userInfo valueForKey:@"aps"] valueForKey:@"alert"] != nil) {
        NSString *message = [[userInfo valueForKey:@"aps"] valueForKey:@"alert"];
        if(message != nil) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Usage Alert"
            message:message  delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
            [alertView show];
            [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0];
            [[UIApplication sharedApplication] cancelAllLocalNotifications];

        }
    }
}

Upvotes: 1

Views: 4036

Answers (3)

CodeShift
CodeShift

Reputation: 51

I want to warn against calling

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:bg_NUM]

from

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

method!

If your scheduled local notification with some badge number, then badge will be set asynchronously some milliseconds after enter to -didReceiveLocalNotification

for sample:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    // ^^^ maybe not reset badge to 0!! ^^^
}

another code:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    sleep(1); //waiting for system is set our scheduled badge
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    // ^^^ most chances for reset badge to 0 ^^^
}

Testing code, screen touches schedule local notifications and calculate delay before system real set badge:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    UILocalNotification *localNotif = [[[UILocalNotification alloc] init] autorelease];
    localNotif.applicationIconBadgeNumber = rand()%100+1;
    localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
[...]
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
    while ([UIApplication sharedApplication].applicationIconBadgeNumber == 0) sleep(0);
        NSLog(@"badge set: %d   after %f sec.", [UIApplication sharedApplication].applicationIconBadgeNumber, CFAbsoluteTimeGetCurrent()-startTime);
}

output:

badge set: 41   after 0.000839 sec.
badge set: 9   after 0.000754 sec.
badge set: 56   after 0.076026 sec.
badge set: 17   after 0.069889 sec.
badge set: 8   after 0.056245 sec.
badge set: 71   after 0.120729 sec.
badge set: 28   after 0.122720 sec.
badge set: 17   after 0.000758 sec.

this testing in iOS 4.2/4.3/5.0/6.1 on different devices

Be careful when you reset badge number in -didReceiveLocalNotification message! (this true only for LocalNotification /not remote push/ and only if application is active on receive moment)

Upvotes: 1

Anil Varghese
Anil Varghese

Reputation: 42977

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{


 UIApplication *app = [UIApplication sharedApplication];
 NSInteger badgeNumber = [app applicationIconBadgeNumber];// Take the current badge number
 badgeNumber--;    // decrement by one 
 [app setApplicationIconBadgeNumber:badgeNumber];  // set ne badge number
 [app cancelLocalNotification:notification];    // cancel the received notification. It will clear the notification from banner alos
}

Upvotes: 5

Dos43
Dos43

Reputation: 128

You can add

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

to your app delegate. This will be called and there you can use

[[UIApplication sharedApplication] cancelLocalNotification:notification]; 

to remove the specific notification and decrement the badge count.

Upvotes: 1

Related Questions