9

Is there any way to handle the push notification from the Notification Center after being tap, and remove it when my application has already launched?

Vincent Bacalso
  • 2,071
  • 4
  • 23
  • 34
  • Same here - For some reason - I see them stay there like unread emails... I saw that they have some expiry time or something - I would assume maybe a day or so... if I find something I will update it here... but if you find anything let me know... – vivianaranha Dec 16 '11 at 17:00
  • sorry @DennisMadsen, haven't found it yet. anyway a lot of apps have this behavior, they won't be removed right away when viewed – Vincent Bacalso Jan 02 '12 at 03:07

1 Answers1

6

I know this is hack and slash, but you can clear all notifications by changing the badge number on your application.

- (void)application:(UIApplication*)application didReceiveRemoteNotification (NSDictionary*)payload
{
    NSLog(@"Received notification: %@", payload);
    //swapping between two badge numbers to clear notifications
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    ...
}

If you already had a badge number you don't want to lose (above example will simply clear badge number in the end) you can do something like

- (void)application:(UIApplication*)application didReceiveRemoteNotification (NSDictionary*)payload
{
    NSLog(@"Received notification: %@", payload);
    /*
     storing current badge number then swapping between 2 values to make sure we 
     clear the badge number. Once this is done set badge number back to original 
     value.
    */
    int badgeNum = [[UIApplication sharedApplication] applicationIconBadgeNumber]
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:badgeNum];
    ...
}

This may not be best practice, but it gets the job done and the client will not know the difference. I like to call it a temp. fix until I stumble upon a better solution. Hope this helps someone!

NicholasTGD
  • 1,256
  • 1
  • 10
  • 11
  • 1
    I had to add [[UIApplication sharedApplication] cancelAllLocalNotifications]; to get it to work. – Roger C S Wernersson Jan 15 '13 at 15:45
  • Well I did say it was a temp fix =) http://stackoverflow.com/questions/9557132/removing-badge-from-ios-app-icon Looks to have a better answer, good luck! – NicholasTGD Mar 20 '13 at 16:54
  • Maybe you have a trick answer for this question? http://stackoverflow.com/questions/26499116/conditionally-dismiss-remote-notification-from-didreceiveremotenotification – k06a Oct 22 '14 at 09:53