18

I have a server that sends me push notifications and let's say that I have 5 notifications on my phone. If I open one of them all other notifications disappears. I want only the one clicked to disappear.

This is how I handle receiving notifications:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    if ( application.applicationState == UIApplicationState.Inactive || application.applicationState == UIApplicationState.Background  )
    {
        // navigating user to a view controller
    }
    application.applicationIconBadgeNumber = 0
}
Steffen D. Sommer
  • 2,896
  • 2
  • 24
  • 47
Almis
  • 3,684
  • 2
  • 28
  • 58
  • There is no way to remove a specific notification as of iOS SDK 5.0. When you tap on notification of your app so they don't show in the Notification Center. – Parth Dabhi May 14 '15 at 08:54
  • It is because of application.applicationIconBadgeNumber = 0,remove that code.Also not sure only the one clicked to disappear. – Mukesh May 14 '15 at 08:56
  • 1
    @muku by application.applicationIconBadgeNumber = 0 is only change badge number of application not notification. – Parth Dabhi May 14 '15 at 08:58
  • @ParthDabhi if u set this code application.applicationIconBadgeNumber = 0 then ur badge number and notifications from notification center are all cleared – Mukesh May 14 '15 at 09:00
  • 1
    @muku notification not clear by that, only app badge you can set. you can change badge number without any notification olso see here [http://stackoverflow.com/questions/28042898/ios-8-change-app-badge-number-without-any-notifications] – Parth Dabhi May 14 '15 at 09:04
  • Possible duplicate of [How to prevent a notification disappear from notification center when another one is opened? iOS](https://stackoverflow.com/questions/45174124/how-to-prevent-a-notification-disappear-from-notification-center-when-another-on) – Ido Oct 10 '19 at 08:49

3 Answers3

20

By setting the applicationIconBadgeNumber to 0, you also remove every notification from the notification center.

This has also been discussed here: iOS application: how to clear notifications?

Furthermore, it is not possible to programmatically remove a single notification, but from iOS8 on, the OS will handle this for you when a user taps a single notification. This has also been discussed here: Remove single remote notification from Notification Center

Community
  • 1
  • 1
Steffen D. Sommer
  • 2,896
  • 2
  • 24
  • 47
2

If you use OneSignal SDK in your app, and you have the problem of disappearing notifications after opening one, you must add key OneSignal_disable_badge_clearing to the Info.plist file in Xcode, as a Boolean type set to YES.

More info here https://documentation.onesignal.com/docs/badges

Ely
  • 8,259
  • 1
  • 54
  • 67
0

The solution is to set the applicationIconBadgeNumber to the real number of notifications the user has in the notification center. I made a function for this:

func updateIconBadge() {
    UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
        DispatchQueue.main.async {
            UIApplication.shared.applicationIconBadgeNumber = notifications.count
        }
    }
}

For more info (like where to call to this function) you may want to check my original answer here: https://stackoverflow.com/a/54296486/8157190

Ido
  • 473
  • 4
  • 16