How do I prevent notification from the action center when opening another? IOS

I am developing an application that receives push notifications. These are push notifications, each of which contains valuable information that appears when the user opens the app from it.

My problem is that if the user receives multiple notifications, if the user deletes it and opens the app, all others disappear from the notification center and I lost all other important information.

I want to be able to keep / prevent notifications from disappearing from the action center to give the user the ability to open them from the action center. Kind of like YouTube notifications. I've even seen this behavior in the Twitch app notifications.

Any idea? Thank.

+4


source to share


1 answer


I know this is a pretty old question, but since it doesn’t have an answer, I’ll tell you how I solved this problem.

In short, the problem is caused by the setting UIApplication.shared.applicationIconBadgeNumber

to 0; this makes all notifications to be removed from the action center.

The solution is to set for the applicationIconBadgeNumber

actual number of notifications the user has in the action center. I made a function for this:



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

      

Now you can call this function in application(_application:, didFinishLaunchingWithOptions:)

the methods application(_application:, didFinishLaunchingWithOptions:)

, applicationWillEnterForeground(_application:)

, applicationDidBecomeActive(_application:)

in AppDelegate.swift

order to ensure that it will be updated when it should be.

0


source







All Articles